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

export interface ApprovalRequest {
  id: number;
  approval_type: string;
  status: 'pending' | 'approved' | 'rejected' | 'cancelled';
  resource_type: string;
  resource_id: number;
  requested_by: number;
  requested_at: string;
  approved_by?: number;
  approved_at?: string;
  rejection_reason?: string;
  priority: string;
  notes?: string;
  request_data?: Record<string, unknown>;
  current_data?: Record<string, unknown>;
}

export interface ApprovalFilters {
  approval_type?: string;
  page?: number;
  limit?: number;
}

class ApprovalService {
  async getPendingApprovals(filters?: ApprovalFilters) {
    const response = await apiClient.get('/approvals/pending', { params: filters });
    return response.data;
  }

  async approveRequest(requestId: number, notes?: string) {
    const response = await apiClient.post(`/approvals/${requestId}/approve`, { notes });
    return response.data;
  }

  async rejectRequest(requestId: number, rejectionReason: string) {
    const response = await apiClient.post(`/approvals/${requestId}/reject`, { rejection_reason: rejectionReason });
    return response.data;
  }

  async getMyRequests(filters?: { status?: string; page?: number; limit?: number }) {
    const response = await apiClient.get('/approvals/my-requests', { params: filters });
    return response.data;
  }

  async cancelRequest(requestId: number) {
    const response = await apiClient.delete(`/approvals/${requestId}`);
    return response.data;
  }
}

export default new ApprovalService();

