import { describe, test, expect } from "bun:test";
import { ActivityController } from "../../../src/controllers/ActivityController";
import { buildMockCWActivity } from "../../setup";

describe("ActivityController", () => {
  // -------------------------------------------------------------------
  // Constructor
  // -------------------------------------------------------------------
  describe("constructor", () => {
    test("sets all public properties from CW data", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.cwActivityId).toBe(5001);
      expect(ctrl.name).toBe("Test Activity");
      expect(ctrl.notes).toBe("Activity notes");
    });

    test("maps type reference", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.typeCwId).toBe(1);
      expect(ctrl.typeName).toBe("Call");
    });

    test("maps status reference", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.statusCwId).toBe(2);
      expect(ctrl.statusName).toBe("Open");
    });

    test("maps company reference", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.companyCwId).toBe(123);
      expect(ctrl.companyName).toBe("Test Company");
      expect(ctrl.companyIdentifier).toBe("TestCo");
    });

    test("maps contact reference", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.contactCwId).toBe(200);
      expect(ctrl.contactName).toBe("Jane Doe");
    });

    test("maps opportunity reference", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.opportunityCwId).toBe(1001);
      expect(ctrl.opportunityName).toBe("Test Opportunity");
    });

    test("maps assignTo reference", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.assignToCwId).toBe(10);
      expect(ctrl.assignToName).toBe("John Roberts");
      expect(ctrl.assignToIdentifier).toBe("jroberts");
    });

    test("maps dates correctly", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.dateStart).toBeInstanceOf(Date);
      expect(ctrl.dateEnd).toBeInstanceOf(Date);
    });

    test("maps _info dates", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.cwLastUpdated).toBeInstanceOf(Date);
      expect(ctrl.cwDateEntered).toBeInstanceOf(Date);
      expect(ctrl.cwEnteredBy).toBe("jroberts");
      expect(ctrl.cwUpdatedBy).toBe("jroberts");
    });

    test("handles null optional fields gracefully", () => {
      const ctrl = new ActivityController(
        buildMockCWActivity({
          type: undefined,
          status: undefined,
          company: undefined,
          contact: undefined,
          opportunity: undefined,
          assignTo: undefined,
          dateStart: undefined,
          dateEnd: undefined,
          notes: undefined,
          _info: {},
        }),
      );
      expect(ctrl.typeCwId).toBeNull();
      expect(ctrl.typeName).toBeNull();
      expect(ctrl.statusCwId).toBeNull();
      expect(ctrl.companyCwId).toBeNull();
      expect(ctrl.contactCwId).toBeNull();
      expect(ctrl.opportunityCwId).toBeNull();
      expect(ctrl.assignToCwId).toBeNull();
      expect(ctrl.dateStart).toBeNull();
      expect(ctrl.dateEnd).toBeNull();
      expect(ctrl.notes).toBeNull();
      expect(ctrl.cwLastUpdated).toBeNull();
    });

    test("maps phoneNumber and email", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.phoneNumber).toBe("555-1234");
      expect(ctrl.email).toBe("jane@test.com");
    });

    test("maps notifyFlag", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.notifyFlag).toBe(false);
    });

    test("maps customFields", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.customFields).toEqual([]);
    });

    test("maps mobileGuid", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      expect(ctrl.mobileGuid).toBe("guid-abc123");
    });
  });

  // -------------------------------------------------------------------
  // toJson
  // -------------------------------------------------------------------
  describe("toJson()", () => {
    test("returns cwActivityId", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      const json = ctrl.toJson();
      expect(json.cwActivityId).toBe(5001);
    });

    test("returns name and notes", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      const json = ctrl.toJson();
      expect(json.name).toBe("Test Activity");
      expect(json.notes).toBe("Activity notes");
    });

    test("formats type as reference object", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      const json = ctrl.toJson();
      expect(json.type).toEqual({ id: 1, name: "Call" });
    });

    test("type is null when no type set", () => {
      const ctrl = new ActivityController(
        buildMockCWActivity({ type: undefined }),
      );
      const json = ctrl.toJson();
      expect(json.type).toBeNull();
    });

    test("formats company as reference object with identifier", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      const json = ctrl.toJson();
      expect(json.company).toEqual({
        id: 123,
        identifier: "TestCo",
        name: "Test Company",
      });
    });

    test("formats assignTo as reference object with identifier", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      const json = ctrl.toJson();
      expect(json.assignTo).toEqual({
        id: 10,
        identifier: "jroberts",
        name: "John Roberts",
      });
    });

    test("formats opportunity as reference object", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      const json = ctrl.toJson();
      expect(json.opportunity).toEqual({
        id: 1001,
        name: "Test Opportunity",
      });
    });

    test("includes dates and meta", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      const json = ctrl.toJson();
      expect(json.dateStart).toBeInstanceOf(Date);
      expect(json.dateEnd).toBeInstanceOf(Date);
      expect(json.cwLastUpdated).toBeInstanceOf(Date);
      expect(json.cwDateEntered).toBeInstanceOf(Date);
      expect(json.cwEnteredBy).toBe("jroberts");
      expect(json.cwUpdatedBy).toBe("jroberts");
    });

    test("includes customFields array", () => {
      const ctrl = new ActivityController(buildMockCWActivity());
      const json = ctrl.toJson();
      expect(json.customFields).toEqual([]);
    });
  });
});
