import { describe, it, expect } from "vitest";
import { filterMeasureRows } from "./measureFilters";
import type { ClassifiedDeal } from "./types";

function makeDeal(overrides: Partial<ClassifiedDeal> = {}): ClassifiedDeal {
  return {
    id: "1",
    dealId: "deal-1",
    dealname: "1 Test Street",
    dealstage: null,
    companyId: null,
    projectCode: null,
    landlordPropertyId: "LP001",
    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: "Loft insulation;CWI",
    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: "Solar PV",
    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(),
    displayStage: "Coordination in Progress",
    ...overrides,
  };
}

describe("filterMeasureRows", () => {
  it("returns all rows when search is empty", () => {
    const deals = [makeDeal(), makeDeal({ dealId: "deal-2" })];
    expect(filterMeasureRows(deals, {}, "")).toEqual(deals);
  });

  it("matches by address (dealname)", () => {
    const match = makeDeal({ dealname: "12 Oak Lane" });
    const noMatch = makeDeal({ dealId: "deal-2", dealname: "5 Elm Street" });
    const result = filterMeasureRows([match, noMatch], {}, "oak");
    expect(result).toEqual([match]);
  });

  it("matches by landlordPropertyId", () => {
    const match = makeDeal({ landlordPropertyId: "LP999" });
    const noMatch = makeDeal({ dealId: "deal-2", landlordPropertyId: "LP001" });
    const result = filterMeasureRows([match, noMatch], {}, "lp999");
    expect(result).toEqual([match]);
  });

  it("matches by proposed measure name", () => {
    const match = makeDeal({ proposedMeasures: "ASHP;Loft insulation" });
    const noMatch = makeDeal({ dealId: "deal-2", proposedMeasures: "CWI" });
    const result = filterMeasureRows([match, noMatch], {}, "ashp");
    expect(result).toEqual([match]);
  });

  it("matches by instructed measure name", () => {
    const match = makeDeal({ dealId: "deal-1" });
    const noMatch = makeDeal({ dealId: "deal-2", proposedMeasures: "CWI" });
    const instructedByDeal = { "deal-1": ["EWI"] };
    const result = filterMeasureRows([match, noMatch], instructedByDeal, "ewi");
    expect(result).toEqual([match]);
  });

  it("matches by technically approved measure name", () => {
    const match = makeDeal({ technicalApprovedMeasuresForInstall: "Battery" });
    const noMatch = makeDeal({ dealId: "deal-2", technicalApprovedMeasuresForInstall: null });
    const result = filterMeasureRows([match, noMatch], {}, "battery");
    expect(result).toEqual([match]);
  });

  it("is case-insensitive", () => {
    const deal = makeDeal({ proposedMeasures: "Loft insulation" });
    expect(filterMeasureRows([deal], {}, "LOFT")).toEqual([deal]);
    expect(filterMeasureRows([deal], {}, "loft insulation")).toEqual([deal]);
  });

  it("returns empty when nothing matches", () => {
    const deal = makeDeal({ dealname: "1 Test St", proposedMeasures: "CWI" });
    expect(filterMeasureRows([deal], {}, "ASHP")).toEqual([]);
  });
});
