import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import {
  ChamadoListResponseSchema,
  ChamadoSchema,
  type CreateChamadoDto,
  type AddMensagemDto,
  type ResolverChamadoDto,
  type Chamado,
  type ChamadoListResponse,
} from '@sar/api-interface';
import { apiFetch } from '../api-client';

export function useChamados() {
  return useQuery<ChamadoListResponse>({
    queryKey: ['chamados'],
    queryFn: async () => ChamadoListResponseSchema.parse(await apiFetch('/chamados')),
  });
}

export function useChamado(id: number) {
  return useQuery<Chamado>({
    queryKey: ['chamados', id],
    queryFn: async () => ChamadoSchema.parse(await apiFetch(`/chamados/${id}`)),
    enabled: id > 0,
  });
}

export function useCreateChamado() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (dto: CreateChamadoDto): Promise<Chamado> =>
      apiFetch('/chamados', { method: 'POST', body: dto }).then((r) => ChamadoSchema.parse(r)),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['chamados'] }),
  });
}

export function useAddMensagemRep(id: number) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (dto: AddMensagemDto): Promise<Chamado> =>
      apiFetch(`/chamados/${id}/mensagens`, { method: 'POST', body: dto }).then((r) =>
        ChamadoSchema.parse(r),
      ),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['chamados', id] });
      qc.invalidateQueries({ queryKey: ['chamados'] });
    },
  });
}

export function useCancelChamado() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number): Promise<Chamado> =>
      apiFetch(`/chamados/${id}/cancelar`, { method: 'PATCH' }).then((r) => ChamadoSchema.parse(r)),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['chamados'] }),
  });
}

// Gerente
export function useChamadosGer() {
  return useQuery<ChamadoListResponse>({
    queryKey: ['ger', 'chamados'],
    queryFn: async () => ChamadoListResponseSchema.parse(await apiFetch('/ger/chamados')),
  });
}

export function useChamadoGer(id: number) {
  return useQuery<Chamado>({
    queryKey: ['ger', 'chamados', id],
    queryFn: async () => ChamadoSchema.parse(await apiFetch(`/ger/chamados/${id}`)),
    enabled: id > 0,
  });
}

export function useAddMensagemEmpresa(id: number) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (dto: AddMensagemDto): Promise<Chamado> =>
      apiFetch(`/ger/chamados/${id}/mensagens`, { method: 'POST', body: dto }).then((r) =>
        ChamadoSchema.parse(r),
      ),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['ger', 'chamados', id] });
      qc.invalidateQueries({ queryKey: ['ger', 'chamados'] });
    },
  });
}

export function useResolverChamado(id: number) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (dto: ResolverChamadoDto): Promise<Chamado> =>
      apiFetch(`/ger/chamados/${id}/resolver`, { method: 'PATCH', body: dto }).then((r) =>
        ChamadoSchema.parse(r),
      ),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['ger', 'chamados', id] });
      qc.invalidateQueries({ queryKey: ['ger', 'chamados'] });
    },
  });
}
