import { describe, expect, it } from "vitest";
import { getCropPlanValidationIssues, isCropPlanComplete } from "./crop-plan-validation";

const completeCrop = {
  crop: "cotton",
  areaHa: "2",
  expectedYield: "5",
};

describe("isCropPlanComplete", () => {
  it("returns true when a crop entry is complete and within land area", () => {
    expect(isCropPlanComplete([completeCrop], 2.5)).toBe(true);
  });

  it("returns false when expected yield is missing", () => {
    expect(isCropPlanComplete([{ ...completeCrop, expectedYield: "" }], 2.5)).toBe(false);
  });

  it("returns false when crop area exceeds declared land", () => {
    expect(isCropPlanComplete([{ ...completeCrop, areaHa: "3" }], 2.5)).toBe(false);
  });
});

describe("getCropPlanValidationIssues", () => {
  it("lists missing fields per crop entry", () => {
    expect(getCropPlanValidationIssues([{ crop: "", areaHa: "", expectedYield: "" }], 0)).toEqual([
      { kind: "missing", cropIndex: 0, field: "crop" },
      { kind: "missing", cropIndex: 0, field: "areaHa" },
      { kind: "missing", cropIndex: 0, field: "expectedYield" },
    ]);
  });

  it("reports area exceeded separately", () => {
    expect(getCropPlanValidationIssues([completeCrop], 1)).toEqual([
      { kind: "areaExceeded", cropAreaSum: 2, totalLandHa: 1 },
    ]);
  });
});
