/**
 * Live Tracking — Domna Survey editor (issue #256)
 *
 * Verifies the approver flow on the Domna section of the property detail
 * drawer:
 *   1. an approver can set a Domna survey type (free text) and date and
 *      save them,
 *   2. the drawer reflects the saved values immediately (optimistic
 *      update),
 *   3. the values persist across a page reload (i.e. the deal-properties
 *      endpoint wrote them server-side).
 *
 * Mirrors `halted-state.cy.js`. Assumes an authenticated approver session
 * is reusable by the test harness; the target portfolio + a deal whose
 * Domna section is editable by the current user are read from Cypress
 * env vars so the spec stays portable.
 */

const PORTFOLIO_SLUG = Cypress.env("LIVE_PORTFOLIO_SLUG");
const TARGET_DEAL_NAME = Cypress.env("LIVE_DOMNA_DEAL_NAME");

const SURVEY_TYPE = "Standard";
const SURVEY_DATE = "2025-07-15";

describe("Domna survey editor — approver flow", function () {
  before(function () {
    if (!PORTFOLIO_SLUG) {
      cy.log(
        "LIVE_PORTFOLIO_SLUG env var not set — skipping live tracking specs",
      );
      this.skip();
    }
  });

  function openDrawerForTargetDeal() {
    cy.visit(`/portfolio/${PORTFOLIO_SLUG}/your-projects/live`);

    // Switch to the Measures tab — the easiest way into the drawer.
    cy.contains("button, [role=tab]", "Measures").click();

    if (TARGET_DEAL_NAME) {
      cy.contains("[data-testid=measures-row]", TARGET_DEAL_NAME).click();
    } else {
      cy.get("[data-testid=measures-row]").first().click();
    }

    cy.get("[data-testid=property-detail-drawer]").should("be.visible");
    // Navigate to Survey & Admin tab (drawer opens on Works tab from Measures row click).
    cy.get("[data-testid=drawer-tab-survey-admin]").click();
    cy.get("[data-testid=drawer-section-domna]").should("exist");
  }

  it("lets an approver set domna survey type + date and persists them across reload", () => {
    openDrawerForTargetDeal();

    // Approver sees editable inputs.
    cy.get("[data-testid=domna-survey-type-input]").should("be.visible");
    cy.get("[data-testid=domna-survey-date-input]").should("be.visible");

    cy.get("[data-testid=domna-survey-type-input]")
      .clear()
      .type(SURVEY_TYPE);
    cy.get("[data-testid=domna-survey-date-input]")
      .clear()
      .type(SURVEY_DATE);

    cy.get("[data-testid=domna-save-button]")
      .should("not.be.disabled")
      .click();

    // Save completes — button label flips back, no error banner.
    cy.get("[data-testid=domna-save-button]").should(
      "contain.text",
      "Save Domna Survey",
    );
    cy.get("[data-testid=domna-error]").should("not.exist");

    // Optimistic update — the inputs already reflect the new values.
    cy.get("[data-testid=domna-survey-type-input]").should(
      "have.value",
      SURVEY_TYPE,
    );
    cy.get("[data-testid=domna-survey-date-input]").should(
      "have.value",
      SURVEY_DATE,
    );

    // Reload the page and reopen the drawer — the persisted values must
    // still be there.
    cy.reload();
    openDrawerForTargetDeal();

    cy.get("[data-testid=domna-survey-type-input]").should(
      "have.value",
      SURVEY_TYPE,
    );
    cy.get("[data-testid=domna-survey-date-input]").should(
      "have.value",
      SURVEY_DATE,
    );
  });
});
