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 PaymentDTO = {
  id: string;
  payment_no: string;
  payment_type: string;
  payer_type: string;
  payer_id: string | null;
  payee_type: string;
  payee_id: string | null;
  amount: number;
  currency: string;
  payment_method: string;
  status: string;
  reference_no: string | null;
  description: string | null;
  created_at: string;
  updated_at: string;
};

export type PaymentCreateDTO = {
  payment_type: string;
  payer_type: string;
  payer_id?: string | null;
  payee_type: string;
  payee_id?: string | null;
  amount: number;
  currency: string;
  payment_method: string;
  reference_no?: string | null;
  description?: string | null;
};

export type PaymentUpdateDTO = {
  amount?: number;
  payment_method?: string;
  reference_no?: string | null;
  description?: string | null;
};

export const usePaymentsBase = createEntityHook<PaymentDTO, PaymentCreateDTO, PaymentUpdateDTO>(
  i18n.t('hooks.usePayments.text_1'),
  ENDPOINTS.PAYMENTS.LIST,
);

export function usePayments() {
  const base = usePaymentsBase();

  const submitForReview = useCallback(async (id: string, note?: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.PAYMENTS.SUBMIT(id), { note });
      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 approvePayment = useCallback(async (id: string, note?: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.PAYMENTS.APPROVE(id), { note });
      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 cancelPayment = useCallback(async (id: string, reason: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.PAYMENTS.CANCEL(id), { 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 postPayment = useCallback(async (id: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.PAYMENTS.POST(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 reversePayment = useCallback(async (id: string, reason: string) => {
    base.setIsLoading(true);
    base.setError(null);
    try {
      const res = await api.post(ENDPOINTS.PAYMENTS.REVERSE(id), { 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,
    submitForReview,
    approvePayment,
    cancelPayment,
    postPayment,
    reversePayment,
  };
}
