import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { AddressNotFoundError, InvalidCountryCodeError } from "../lib/errors.generated";
import { baseBillingAddress } from "../testing/fixtures";
import { run } from "./updatePartnerAddress";

describe("updatePartnerAddress", () => {
  it("updates address fields", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updatedAddress = {
      ...baseBillingAddress,
      line1: "789 Elm St",
      updatedAt: new Date(),
    };

    spies.select.mockReturnValueOnce(baseBillingAddress); // Address lookup
    spies.update.mockReturnValue(updatedAddress);

    const result = await run(db, {
      id: "address-1",
      line1: "789 Elm St",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.address.line1).toBe("789 Elm St");
    }
    expect(spies.update).toHaveBeenCalled();
  });

  it("updates address for DRAFT partner", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const draftAddress = { ...baseBillingAddress, partnerId: "partner-1" };
    const updatedAddress = {
      ...draftAddress,
      city: "Boston",
      updatedAt: new Date(),
    };

    spies.select.mockReturnValueOnce(draftAddress);
    spies.update.mockReturnValue(updatedAddress);

    const result = await run(db, {
      id: "address-1",
      city: "Boston",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.address.city).toBe("Boston");
    }
    expect(spies.update).toHaveBeenCalled();
  });

  it("updates address for ACTIVE partner", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const activeAddress = { ...baseBillingAddress, partnerId: "partner-2" };
    const updatedAddress = {
      ...activeAddress,
      postalCode: "10002",
      updatedAt: new Date(),
    };

    spies.select.mockReturnValueOnce(activeAddress);
    spies.update.mockReturnValue(updatedAddress);

    const result = await run(db, {
      id: "address-1",
      postalCode: "10002",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.address.postalCode).toBe("10002");
    }
    expect(spies.update).toHaveBeenCalled();
  });

  it("updates address for INACTIVE partner", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const inactiveAddress = { ...baseBillingAddress, partnerId: "partner-3" };
    const updatedAddress = {
      ...inactiveAddress,
      line1: "999 Pine Rd",
      updatedAt: new Date(),
    };

    spies.select.mockReturnValueOnce(inactiveAddress);
    spies.update.mockReturnValue(updatedAddress);

    const result = await run(db, {
      id: "address-1",
      line1: "999 Pine Rd",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.address.line1).toBe("999 Pine Rd");
    }
    expect(spies.update).toHaveBeenCalled();
  });

  it("returns error when country code is lowercase", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseBillingAddress);

    const result = await run(db, {
      id: "address-1",
      country: "us",
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(InvalidCountryCodeError);
    }
  });

  it("returns error when address not found", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined); // Address not found

    const result = await run(db, {
      id: "nonexistent",
      line1: "789 Elm St",
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(AddressNotFoundError);
    }
  });

  it("returns error when country code is invalid", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseBillingAddress); // Address exists

    const result = await run(db, {
      id: "address-1",
      country: "USA",
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(InvalidCountryCodeError);
    }
  });
});
