import { getTemplate } from "@/record-management/services/api/templateService";
import { useQuery } from "@tanstack/react-query";

export interface NameDTO {
  am: string;
  en: string;
}

export interface LetterTemplateDto {
  id: string;
  createdAt: string;
  updatedAt: string;
  name: NameDTO;
  key: string;
  subject: string;
  body: string;
  sincerelyText: string;
  unitId: string;
}

export interface LetterTemplateListResponseDTO {
  count: number;
  items: LetterTemplateDto[];
}

const useTemplate = (unitId: string) => {
  const { data, isLoading, isError, refetch } =
    useQuery<LetterTemplateListResponseDTO>({
      queryKey: ["message"],
      queryFn: () => getTemplate(unitId as string, { take: 200 }),
      staleTime: 10 * 60 * 1000,
    });
  const templates = data?.items || [];

  return { templates, isLoading, isError, refetch };
};

export default useTemplate;
