import { useCallback } from 'react';
import { createEntityHook } from './useEntityBase';
import { api } from '@/lib/api';
import { ENDPOINTS } from '@/constants/endpoints';
import i18n from "@/lib/i18n";

export type IncidentDTO = {
  id: string;
  incident_number: string;
  incident_type: string;
  severity: string;
  status: string;
  bar_id: string | null;
  vault_id: string | null;
  branch_id: string | null;
  stock_count_id: string | null;
  description: string | null;
  resolution: string | null;
  reported_by: string | null;
  reported_at: string;
  investigated_by: string | null;
  investigated_at: string | null;
  resolved_by: string | null;
  resolved_at: string | null;
  notes: string | null;
  created_at: string;
  updated_at: string;
};

export type IncidentCreateDTO = {
  incident_number: string;
  incident_type: string;
  severity?: string;
  bar_id?: string | null;
  vault_id?: string | null;
  branch_id?: string | null;
  stock_count_id?: string | null;
  description?: string | null;
};

export type IncidentUpdateDTO = {
  severity?: string | null;
  status?: string | null;
  resolution?: string | null;
  notes?: string | null;
};

export const useIncidentsBase = createEntityHook<IncidentDTO, IncidentCreateDTO, IncidentUpdateDTO>(
  i18n.t('hooks.useIncidents.text_1'),
  ENDPOINTS.INCIDENTS.LIST,
);

export function useIncidents() {
  const base = useIncidentsBase();

  const investigateIncident = useCallback(async (id: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.INCIDENTS.INVESTIGATE(id));
      if (res.success) {
        await base.fetchItems(base.currentPage, base.pageSize, base.searchTerm);
        return true;
      }
      return false;
    } catch (err: any) {
      base.setError(err.message);
      return false;
    } finally {
      base.setIsLoading(false);
    }
  }, [base]);

  const resolveIncident = useCallback(async (id: string, resolution: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.INCIDENTS.RESOLVE(id) + `?resolution=${encodeURIComponent(resolution)}`);
      if (res.success) {
        await base.fetchItems(base.currentPage, base.pageSize, base.searchTerm);
        return true;
      }
      return false;
    } catch (err: any) {
      base.setError(err.message);
      return false;
    } finally {
      base.setIsLoading(false);
    }
  }, [base]);

  const closeIncident = useCallback(async (id: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.INCIDENTS.CLOSE(id));
      if (res.success) {
        await base.fetchItems(base.currentPage, base.pageSize, base.searchTerm);
        return true;
      }
      return false;
    } catch (err: any) {
      base.setError(err.message);
      return false;
    } finally {
      base.setIsLoading(false);
    }
  }, [base]);

  const fetchLinkedAdjustments = useCallback(async (incidentId: string) => {
    try {
      const res = await api.get(`${ENDPOINTS.ADJUSTMENTS.LIST}/by-incident/${incidentId}`);
      return (res as any)?.data || res || [];
    } catch (err) {
      console.error(err);
      return [];
    }
  }, []);

  const createLinkedAdjustment = useCallback(async (payload: any) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.ADJUSTMENTS.CREATE, payload);
      if (res.success) {
        return true;
      }
      return false;
    } catch (err: any) {
      base.setError(err.message);
      return false;
    } finally {
      base.setIsLoading(false);
    }
  }, [base]);

  return {
    ...base,
    investigateIncident,
    resolveIncident,
    closeIncident,
    fetchLinkedAdjustments,
    createLinkedAdjustment,
  };
}
