import apiClient from '../../../shared/services/apiClient';

export type ApprovalActionType = 
  | 'large_refund'
  | 'manual_payment_status_override'
  | 'invoice_write_off'
  | 'significant_discount'
  | 'tax_rate_change'
  | 'fiscal_period_close'
  | 'gl_manual_entry';

export type ApprovalStatus = 'pending' | 'approved' | 'rejected' | 'cancelled';

export interface FinancialApproval {
  id: number;
  action_type: ApprovalActionType;
  action_description: string;
  status: ApprovalStatus;
  requested_by: number;
  requested_by_email?: string;
  approved_by?: number;
  approved_by_email?: string;
  requested_at: string;
  responded_at?: string;
  payment_id?: number;
  invoice_id?: number;
  booking_id?: number;
  gl_entry_id?: number;
  amount?: number;
  previous_value?: unknown;
  new_value?: unknown;
  currency?: string;
  request_reason?: string;
  response_notes?: string;
  metadata?: Record<string, unknown>;
}

export interface RespondToApprovalRequest {
  status: 'approved' | 'rejected';
  response_notes?: string;
}

class ApprovalService {
  async getApprovals(params?: {
    status?: ApprovalStatus;
    action_type?: ApprovalActionType;
    requested_by?: number;
    approved_by?: number;
    page?: number;
    limit?: number;
  }): Promise<{ status: string; data: FinancialApproval[] }> {
    const response = await apiClient.get('/financial/approvals', { params });
    return response.data;
  }

  async getApprovalById(approvalId: number): Promise<{ status: string; data: FinancialApproval }> {
    const response = await apiClient.get(`/financial/approvals/${approvalId}`);
    return response.data;
  }

  async respondToApproval(approvalId: number, data: RespondToApprovalRequest): Promise<{ status: string; data: FinancialApproval }> {
    const response = await apiClient.post(`/financial/approvals/${approvalId}/respond`, data);
    return response.data;
  }
}

const approvalService = new ApprovalService();
export default approvalService;

