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 CustodyDTO = {
  id: string;
  bar_id: string;
  client_user_id: string;
  vault_id: string;
  custody_grams: number;
  status: string;
  custody_start: string;
  custody_end: string | null;
  confirmed_by: string | null;
  confirmed_at: string | null;
  released_by: string | null;
  released_by_type: string | null;
  released_reason: string | null;
  reference_type: string | null;
  reference_id: string | null;
  notes: string | null;
  created_at: string;
  updated_at: string;
};

export type CustodyCreateDTO = {
  bar_id: string;
  client_user_id: string;
  vault_id: string;
  custody_grams?: number | null;
  reference_type?: string | null;
  reference_id?: string | null;
  notes?: string | null;
};

export const useCustodyBase = createEntityHook<CustodyDTO, CustodyCreateDTO, never>(
  i18n.t('hooks.useCustody.text_1'),
  ENDPOINTS.CUSTODY.CHAIN,
);

export function useCustody() {
  const base = useCustodyBase();

  const confirmCustody = useCallback(async (id: string, notes?: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.CUSTODY.CONFIRM(id), { notes });
      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 releaseCustody = useCallback(async (id: string, reason?: string, notes?: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.CUSTODY.RELEASE(id), { released_reason: reason, notes });
      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 transferCustody = useCallback(async (id: string, newVaultId: string, reason?: string, notes?: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.CUSTODY.TRANSFER(id), { new_vault_id: newVaultId, reason, notes });
      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 revokeCustody = useCallback(async (id: string, reason: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.CUSTODY.REVOKE(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]);

  return {
    ...base,
    confirmCustody,
    releaseCustody,
    transferCustody,
    revokeCustody,
  };
}
