import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } 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 mock = createKyselyMock<Namespace["main-db"]>();
    const updatedAddress = {
      ...baseBillingAddress,
      line1: "789 Elm St",
      updatedAt: new Date(),
    };

    mock.enqueueResults([baseBillingAddress], [updatedAddress]);

    const result = await mock.withTx((trx) =>
      run(trx, {
        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(mock.updates.length).toBeGreaterThan(0);
  });

  it("updates address for DRAFT partner", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const draftAddress = { ...baseBillingAddress, partnerId: "partner-1" };
    const updatedAddress = {
      ...draftAddress,
      city: "Boston",
      updatedAt: new Date(),
    };

    mock.enqueueResults([draftAddress], [updatedAddress]);

    const result = await mock.withTx((trx) =>
      run(trx, {
        id: "address-1",
        city: "Boston",
      }),
    );

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.address.city).toBe("Boston");
    }
    expect(mock.updates.length).toBeGreaterThan(0);
  });

  it("updates address for ACTIVE partner", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const activeAddress = { ...baseBillingAddress, partnerId: "partner-2" };
    const updatedAddress = {
      ...activeAddress,
      postalCode: "10002",
      updatedAt: new Date(),
    };

    mock.enqueueResults([activeAddress], [updatedAddress]);

    const result = await mock.withTx((trx) =>
      run(trx, {
        id: "address-1",
        postalCode: "10002",
      }),
    );

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.address.postalCode).toBe("10002");
    }
    expect(mock.updates.length).toBeGreaterThan(0);
  });

  it("updates address for INACTIVE partner", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const inactiveAddress = { ...baseBillingAddress, partnerId: "partner-3" };
    const updatedAddress = {
      ...inactiveAddress,
      line1: "999 Pine Rd",
      updatedAt: new Date(),
    };

    mock.enqueueResults([inactiveAddress], [updatedAddress]);

    const result = await mock.withTx((trx) =>
      run(trx, {
        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(mock.updates.length).toBeGreaterThan(0);
  });

  it("returns error when country code is lowercase", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseBillingAddress]);

    const result = await mock.withTx((trx) =>
      run(trx, {
        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 mock = createKyselyMock<Namespace["main-db"]>();
    // Address not found

    const result = await mock.withTx((trx) =>
      run(trx, {
        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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseBillingAddress]); // Address exists

    const result = await mock.withTx((trx) =>
      run(trx, {
        id: "address-1",
        country: "USA",
      }),
    );

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