import { describe, it, expect } from "vitest";
import { formatPropertyCsv, PROPERTY_CSV_FIELDS } from "./propertyCsv";
import type { ClassifiedDeal, HubspotDeal } from "./types";

function makeDeal(overrides: Partial<HubspotDeal> = {}): HubspotDeal {
  return {
    id: "1",
    dealId: "deal-1",
    dealname: "Test Property",
    dealstage: null,
    companyId: null,
    projectCode: null,
    landlordPropertyId: null,
    uprn: null,
    outcome: null,
    outcomeNotes: null,
    majorConditionIssueDescription: null,
    majorConditionIssuePhotos: null,
    majorConditionIssuePhotosS3: null,
    coordinationStatus: null,
    designStatus: null,
    bookingStatus: null,
    pashubLink: null,
    sharepointLink: null,
    dampMouldFlag: null,
    dampMouldAndRepairComments: null,
    preSapScore: null,
    coordinator: null,
    ioeV1Date: null,
    ioeV2Date: null,
    ioeV3Date: null,
    proposedMeasures: null,
    approvedPackage: null,
    designer: null,
    designDate: null,
    actualMeasuresInstalled: null,
    installer: null,
    installerHandover: null,
    lodgementStatus: null,
    measuresLodgementDate: null,
    fullLodgementDate: null,
    confirmedSurveyDate: null,
    confirmedSurveyTime: null,
    surveyedDate: null,
    designType: null,
    eiScore: null,
    eiScorePotential: null,
    epcSapScore: null,
    potentialPostSapScoreDropdown: null,
    epcPrn: null,
    surveyType: null,
    measuresForPibiOrdered: null,
    pibiOrderDate: null,
    pibiCompletedDate: null,
    propertyHaltedDate: null,
    propertyHaltedReason: null,
    technicalApprovedMeasuresForInstall: null,
    domnaSurveyType: null,
    domnaSurveyDate: null,
    batch: null,
    batchDescription: null,
    planningAuthority: null,
    designatedArea: null,
    article4PdRights: null,
    listedBuilding: null,
    designConstraints: null,
    planningComments: null,
    planningStatus: null,
    planningSuggestedApproach: null,
    coordinationComments: null,
    domnasurveyRequired: null,
    createdAt: new Date(),
    updatedAt: new Date(),
    ...overrides,
  };
}

function makeClassified(overrides: Partial<ClassifiedDeal> = {}): ClassifiedDeal {
  return { ...makeDeal(overrides), displayStage: "Scope & Planning", ...overrides };
}

function headerCells(csv: string): string[] {
  return csv.split("\n")[0].split(",");
}

function rowCells(csv: string, rowIndex: number): string[] {
  return csv.split("\n")[rowIndex + 1].split(",");
}

describe("formatPropertyCsv", () => {
  it("includes Surveyed Date and EPC Certificate Number headers, with values for a populated deal", () => {
    const deal = makeClassified({
      surveyedDate: new Date("2026-03-15T00:00:00Z"),
      epcPrn: "1234-5678-9012-3456-7890",
    });

    const csv = formatPropertyCsv([deal]);
    const headers = headerCells(csv);
    const cells = rowCells(csv, 0);

    const surveyedIdx = headers.indexOf("Surveyed Date");
    const prnIdx = headers.indexOf("EPC Certificate Number");

    expect(surveyedIdx).toBeGreaterThan(-1);
    expect(prnIdx).toBeGreaterThan(-1);
    expect(cells[surveyedIdx]).toMatch(/^\d{2}\/\d{2}\/\d{4}$/);
    expect(cells[prnIdx]).toBe("1234-5678-9012-3456-7890");
  });

  it("renders null fields as empty cells", () => {
    const deal = makeClassified({ surveyedDate: null, epcPrn: null });

    const csv = formatPropertyCsv([deal]);
    const headers = headerCells(csv);
    const cells = rowCells(csv, 0);

    expect(cells[headers.indexOf("Surveyed Date")]).toBe("");
    expect(cells[headers.indexOf("EPC Certificate Number")]).toBe("");
  });

  it("emits header row alone when given no rows", () => {
    const csv = formatPropertyCsv([]);
    expect(csv.split("\n")).toHaveLength(1);
    expect(headerCells(csv)).toEqual(PROPERTY_CSV_FIELDS.map((f) => f.label));
  });
});
