/**
 * Live Tracking — Tabbed property detail drawer
 *
 * Verifies the four-tab drawer structure introduced in the UI redesign:
 *   Overview | Works | PIBI | Survey & Admin
 *
 * The spec opens the drawer from the Properties table (first row) and asserts
 * tab presence, default active state, and navigation between tabs.
 */

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

describe("Property detail drawer — tabbed layout", function () {
  before(function () {
    if (!PORTFOLIO_SLUG) {
      cy.log(
        "LIVE_PORTFOLIO_SLUG env var not set — skipping live tracking specs",
      );
      this.skip();
    }
  });

  function openDrawer() {
    cy.visit(`/portfolio/${PORTFOLIO_SLUG}/your-projects/live`);
    cy.contains("button, [role=tab]", "Measures").click();
    cy.get("[data-testid=measures-row]").first().click();
    cy.get("[data-testid=property-detail-drawer]").should("be.visible");
  }

  it("opens with Overview tab active by default", () => {
    openDrawer();

    cy.get("[data-testid=drawer-tab-overview]")
      .should("be.visible")
      .and("have.attr", "aria-selected", "true");

    cy.get("[data-testid=drawer-tab-panel-overview]").should("be.visible");
  });

  it("shows all four tabs", () => {
    openDrawer();

    cy.get("[data-testid=drawer-tab-overview]").should("be.visible");
    cy.get("[data-testid=drawer-tab-works]").should("be.visible");
    cy.get("[data-testid=drawer-tab-pibi]").should("be.visible");
    cy.get("[data-testid=drawer-tab-survey-admin]").should("be.visible");
  });

  it("navigates to Works tab and shows measures content", () => {
    openDrawer();

    cy.get("[data-testid=drawer-tab-works]").click();

    cy.get("[data-testid=drawer-tab-panel-works]").should("be.visible");
    // Measures section lives in Works tab
    cy.get("[data-testid=drawer-section-measures]").should("exist");
  });

  it("navigates to PIBI tab and shows PIBI content", () => {
    openDrawer();

    cy.get("[data-testid=drawer-tab-pibi]").click();

    cy.get("[data-testid=drawer-tab-panel-pibi]").should("be.visible");
    cy.get("[data-testid=drawer-section-pibi]").should("exist");
  });

  it("navigates to Survey & Admin tab and shows admin content", () => {
    openDrawer();

    cy.get("[data-testid=drawer-tab-survey-admin]").click();

    cy.get("[data-testid=drawer-tab-panel-survey-admin]").should("be.visible");
    cy.get("[data-testid=drawer-section-domna]").should("exist");
  });

  it("focusSection=pibi opens PIBI tab directly", () => {
    // This is exercised by the pibi-dates.cy.js helper that clicks the Measures
    // tab row — after the redesign those rows pass focusSection="pibi" and the
    // drawer should land on the PIBI tab, not Overview.
    cy.visit(`/portfolio/${PORTFOLIO_SLUG}/your-projects/live`);

    cy.contains("button, [role=tab]", "Measures").click();
    cy.get("[data-testid=measures-row]").first().click();

    cy.get("[data-testid=property-detail-drawer]").should("be.visible");

    cy.get("[data-testid=drawer-tab-works]")
      .should("have.attr", "aria-selected", "true");
  });
});
