"use client";

import { useMemo } from "react";
import { trpc } from "~/lib/trpc/client.js";

export interface ClientReference {
  id: string;
  name: string;
  code: string | null;
  isActive?: boolean;
}

export interface CountryReference {
  id: string;
  name: string;
  code: string;
  isActive?: boolean;
}

export interface RoleReference {
  id: string;
  name: string;
  isActive?: boolean;
}

export interface ReferenceDataSelection {
  clients?: boolean;
  countries?: boolean;
  roles?: boolean;
  chapters?: boolean;
}

const LOOKUP_STALE_TIME_MS = 300_000;

export function useReferenceData(selection: ReferenceDataSelection = {}) {
  const shouldLoadClients = selection.clients === true;
  const shouldLoadCountries = selection.countries === true;
  const shouldLoadRoles = selection.roles === true;
  const shouldLoadChapters = selection.chapters === true;

  const { data: clientsRaw } = trpc.clientEntity.list.useQuery(
    { isActive: true },
    { staleTime: LOOKUP_STALE_TIME_MS, enabled: shouldLoadClients },
  );

  const { data: countriesRaw } = trpc.country.list.useQuery(
    { isActive: true },
    { staleTime: LOOKUP_STALE_TIME_MS, enabled: shouldLoadCountries },
  );

  const { data: rolesRaw } = trpc.role.list.useQuery(
    { isActive: true },
    { staleTime: LOOKUP_STALE_TIME_MS, enabled: shouldLoadRoles },
  );

  const { data: chaptersRaw } = trpc.resource.chapters.useQuery(undefined, {
    staleTime: LOOKUP_STALE_TIME_MS,
    enabled: shouldLoadChapters,
  });

  const clients = useMemo<ClientReference[]>(() => {
    if (!shouldLoadClients) return [];
    const list = (
      Array.isArray(clientsRaw) ? clientsRaw : ((clientsRaw as { clients?: ClientReference[] } | undefined)?.clients ?? [])
    ) as ClientReference[];
    return [...list]
      .filter((client) => client.isActive !== false)
      .sort((left, right) => left.name.localeCompare(right.name));
  }, [clientsRaw, shouldLoadClients]);

  const countries = useMemo<CountryReference[]>(() => {
    if (!shouldLoadCountries) return [];
    const list = (Array.isArray(countriesRaw) ? countriesRaw : []) as CountryReference[];
    return [...list]
      .filter((country) => country.isActive !== false)
      .sort((left, right) => left.name.localeCompare(right.name));
  }, [countriesRaw, shouldLoadCountries]);

  const roles = useMemo<RoleReference[]>(() => {
    if (!shouldLoadRoles) return [];
    const list = (Array.isArray(rolesRaw) ? rolesRaw : []) as RoleReference[];
    return [...list]
      .filter((role) => role.isActive !== false)
      .sort((left, right) => left.name.localeCompare(right.name));
  }, [rolesRaw, shouldLoadRoles]);

  const chapters = useMemo<string[]>(() => {
    if (!shouldLoadChapters) return [];
    const list = (Array.isArray(chaptersRaw) ? chaptersRaw : []) as string[];
    return [...list].sort((left, right) => left.localeCompare(right));
  }, [chaptersRaw, shouldLoadChapters]);

  return {
    clients,
    countries,
    roles,
    chapters,
  };
}
