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 TransferOrderDTO = {
  id: string;
  order_number: string;
  from_vault_id: string;
  from_vault_name: string | null;
  to_vault_id: string;
  to_vault_name: string | null;
  from_branch_id: string | null;
  to_branch_id: string | null;
  status: string;
  transfer_reason: string | null;
  priority: string;
  scheduled_at: string | null;
  dispatched_at: string | null;
  received_at: string | null;
  requested_by: string | null;
  approved_by: string | null;
  dispatched_by: string | null;
  received_by: string | null;
  total_bars: number;
  total_weight: number;
  notes: string | null;
  created_at: string;
  updated_at: string;
};

export type TransferOrderCreateDTO = {
  order_number?: string | null;
  from_vault_id: string;
  to_vault_id: string;
  from_branch_id?: string | null;
  to_branch_id?: string | null;
  bar_ids: string[];
  transfer_reason?: string | null;
  priority?: string;
  scheduled_at?: string | null;
  notes?: string | null;
};

export type TransferOrderUpdateDTO = {
  transfer_reason?: string | null;
  priority?: string;
  scheduled_at?: string | null;
  notes?: string | null;
};

export const useTransfersBase = createEntityHook<TransferOrderDTO, TransferOrderCreateDTO, TransferOrderUpdateDTO>(
  i18n.t('hooks.useTransfers.text_1'),
  ENDPOINTS.TRANSFERS.LIST,
);

export function useTransfers() {
  const base = useTransfersBase();

  const approveTransfer = useCallback(async (id: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.TRANSFERS.APPROVE(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 rejectTransfer = useCallback(async (id: string, reason: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.TRANSFERS.REJECT(id) + `?reason=${encodeURIComponent(reason)}`);
      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 dispatchTransfer = useCallback(async (id: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.TRANSFERS.DISPATCH(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 completeTransfer = useCallback(async (id: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.TRANSFERS.COMPLETE(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]);

  return {
    ...base,
    approveTransfer,
    rejectTransfer,
    dispatchTransfer,
    completeTransfer,
  };
}