import { describe, it, expect } from "vitest";
import { getContactInfo } from "./getContactInfo.helper";
import { HubSpotContactResults } from "../types";

const MOCK_1 = {
  cuit: "24426590073",
  apellido: "Ghertner",
  nombre: "Tomas",
  emails: [
    {
      direccion: "tomas.ghertner6@yahoo.com.ar",
      estado: "Confirmado",
      tipoEmail: "Personal",
    },
  ],
  telefonos: [],
};

describe("getInfoTesting", () => {
  it("Test 1", () => {
    expect(getContactInfo(MOCK_1, "", null)).toEqual({
      hs_id: "",
      name: "Tomas",
      last_name: "Ghertner",
      cuit: "",
      landline: "",
      mobile: "",
      email: "tomas.ghertner6@yahoo.com.ar",
      email_type: "Personal",
    });
  });

  it("Test 2", () => {
    const MOCK_2 = {
      cuit: "24287517394",
      apellido: "Moretti",
      nombre: "Andrea Graciela",
      emails: [
        {
          direccion: "andreagraciela.moretti5@yahoo.com.ar",
          estado: "No Confirmado por Aplicacion",
          tipoEmail: "Personal",
        },
      ],
      telefonos: [
        {
          numero: "5491171987772",
          tipoLinea: "Fijo",
          tipoTelefono: "Personal",
        },
      ],
    };
    expect(getContactInfo(MOCK_2, "", null)).toEqual({
      hs_id: "",
      name: "Andrea Graciela",
      last_name: "Moretti",
      cuit: "",
      landline: "5491171987772",
      mobile: "",
      email: "andreagraciela.moretti5@yahoo.com.ar",
      email_type: "Personal",
    });
  });

  it("ARCA phones have priority over HubSpot phones", () => {
    const hsInfo: HubSpotContactResults = {
      id: "123",
      properties: {
        firstname: "Tomas",
        lastname: "Ghertner",
        phone: "1111111111",
        mobilephone: "2222222222",
        hs_whatsapp_phone_number: "3333333333",
        email: "",
        hs_object_id: "",
      },
    };

    const arca = {
      ...MOCK_1,
      telefonos: [
        {
          numero: "4444444444",
          tipoLinea: "Fijo",
          tipoTelefono: "Personal",
        },
        {
          numero: "5555555555",
          tipoLinea: "Celular",
          tipoTelefono: "Personal",
        },
      ],
    };

    expect(getContactInfo(arca, "24426590073", hsInfo)).toEqual({
      hs_id: "123",
      name: "Tomas",
      last_name: "Ghertner",
      cuit: "24426590073",
      landline: "4444444444",
      mobile: "5555555555",
      email: "tomas.ghertner6@yahoo.com.ar",
      email_type: "Personal",
    });
  });

  it("uses HubSpot phones when ARCA has no phones", () => {
    const hsInfo: HubSpotContactResults = {
      id: "456",
      properties: {
        firstname: "Andrea",
        lastname: "Moretti",
        phone: "1111111111",
        mobilephone: "2222222222",
        hs_whatsapp_phone_number: "3333333333",
        email: "",
        hs_object_id: "",
      },
    };

    expect(getContactInfo(MOCK_1, "", hsInfo)).toEqual({
      hs_id: "456",
      name: "Andrea",
      last_name: "Moretti",
      cuit: "",
      landline: "1111111111",
      mobile: "2222222222",
      email: "tomas.ghertner6@yahoo.com.ar",
      email_type: "Personal",
    });
  });

  it("uses whatsapp as mobile fallback", () => {
    const hsInfo: HubSpotContactResults = {
      id: "789",
      properties: {
        firstname: "Juan",
        lastname: "Perez",
        phone: "1111111111",
        mobilephone: null,
        hs_whatsapp_phone_number: "3333333333",
        email: "",
        hs_object_id: "",
      },
    };

    expect(getContactInfo(MOCK_1, "", hsInfo)).toEqual({
      hs_id: "789",
      name: "Juan",
      last_name: "Perez",
      cuit: "",
      landline: "1111111111",
      mobile: "3333333333",
      email: "tomas.ghertner6@yahoo.com.ar",
      email_type: "Personal",
    });
  });

  it("handles missing HubSpot properties", () => {
    const hsInfo: HubSpotContactResults = {
      id: "999",
      properties: {
        firstname: "Carlos",
        lastname: "Lopez",
        email: "",
        hs_object_id: "",
      },
    };

    expect(getContactInfo(MOCK_1, "", hsInfo)).toEqual({
      hs_id: "999",
      name: "Carlos",
      last_name: "Lopez",
      cuit: "",
      landline: "",
      mobile: "",
      email: "tomas.ghertner6@yahoo.com.ar",
      email_type: "Personal",
    });
  });
});
