import { API } from '@/api/API';

export const onboardingService = {
    submitApplication: async (data: any) => {
        const response: any = await API.submitApplication(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to submit application');
        return response.data;
    },
    getApplications: async (params?: any) => {
        const response: any = await API.getApplications(params);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to fetch applications');
        return response.data;
    },
    shortlistApplications: async (applicationIds: string[], assignedTo: string[], remarks?: string) => {
        const response: any = await API.shortlistApplications({ applicationIds, assignedTo, remarks });
        if (!response.ok) throw new Error(response.data?.message || 'Failed to shortlist applications');
        return response.data;
    },
    getApplicationById: async (id: string) => {
        const response: any = await API.getApplicationById(id);
        if (!response.ok) {
            console.error('API Error Response:', response.status, response.data);
            throw new Error(response.data?.message || 'Failed to fetch application details');
        }
        return response.data?.data || response.data;
    },
    getUsers: async (params?: any) => {
        const response: any = await API.getUsers(params);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to fetch users');
        return response.data?.data || response.data;
    },
    addParticipant: async (data: any) => {
        const response: any = await API.addParticipant(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to add participant');
        return response.data;
    },
    scheduleInterview: async (data: any) => {
        const response: any = await API.scheduleInterview(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to schedule interview');
        return response.data;
    },
    updateInterview: async (id: string, data: any) => {
        const response: any = await API.updateInterview(id, data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to update interview');
        return response.data;
    },
    getInterviews: async (applicationId: string) => {
        const response: any = await API.getInterviews(applicationId);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to fetch interviews');
        const data = response.data?.data || response.data;
        return Array.isArray(data) ? data : [];
    },
    getDocuments: async (id: string) => {
        const response: any = await API.getDocuments(id);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to fetch documents');
        return response.data?.data || response.data;
    },
    uploadDocument: async (id: string, data: any) => {
        const response: any = await API.uploadDocument(id, data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to upload document');
        return response.data;
    },
    submitKTMatrix: async (data: any) => {
        const response: any = await API.submitKTMatrix(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to submit KT Matrix');
        return response.data;
    },
    submitLevel2Feedback: async (data: any) => {
        const response: any = await API.submitLevel2Feedback(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to submit feedback');
        return response.data;
    },
    updateRecommendation: async (data: any) => {
        const response: any = await API.updateRecommendation(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to update recommendation');
        return response.data;
    },
    submitStageDecision: async (data: any) => {
        const response: any = await API.submitStageDecision(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to process stage decision');
        return response.data;
    },
    updateInterviewDecision: async (data: any) => {
        const response: any = await API.updateInterviewDecision(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to update interview decision');
        return response.data;
    },
    assignArchitectureTeam: async (applicationId: string, assignedTo: string) => {
        const response: any = await API.assignArchitectureTeam(applicationId, assignedTo);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to assign architecture team');
        return response.data;
    },
    updateArchitectureStatus: async (applicationId: string, status: string, remarks?: string) => {
        const response: any = await API.updateArchitectureStatus(applicationId, status, remarks);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to update architecture status');
        return response.data;
    },
    generateDealerCodes: async (applicationId: string) => {
        const response: any = await API.generateDealerCodes(applicationId);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to generate dealer codes');
        return response.data;
    },
    updateApplicationStatus: async (id: string, data: any) => {
        const response: any = await API.updateApplicationStatus(id, data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to update application status');
        return response.data;
    },
    convertToOpportunity: async (id: string, data?: any) => {
        const response: any = await API.convertToOpportunity(id, data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to convert application to opportunity');
        return response.data;
    },
    bulkConvertToOpportunity: async (data: any) => {
        const response: any = await API.bulkConvertToOpportunity(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to perform bulk conversion');
        return response.data;
    },
    sendBulkReminders: async (applicationIds: string[]) => {
        const response: any = await API.sendBulkReminders({ applicationIds });
        if (!response.ok) throw new Error(response.data?.message || 'Failed to send reminders');
        return response.data;
    },
    createDealer: async (data: any) => {
        const response: any = await API.createDealer(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to create dealer profile');
        return response.data;
    },
    retriggerEvaluators: async (id: string) => {
        const response: any = await API.retriggerEvaluators(id);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to retrigger evaluators');
        return response.data;
    },
    getSecurityDeposit: async (applicationId: string) => {
        const response: any = await API.getSecurityDeposit(applicationId);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to fetch security deposit');
        return response.data?.data || response.data;
    },
    updateSecurityDeposit: async (data: any) => {
        const response: any = await API.updateSecurityDeposit(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to update security deposit');
        return response.data;
    },
    getSystemConfigs: async (params?: any) => {
        const response: any = await API.getSystemConfigs(params);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to fetch system configurations');
        return response.data?.data || response.data;
    },
    getDocumentConfigMetadata: async () => {
        const response: any = await API.getDocumentConfigMetadata();
        if (!response.ok) throw new Error(response.data?.message || 'Failed to fetch metadata');
        return response.data?.data || response.data;
    },
    getDocumentConfigs: async (params?: any) => {
        const response: any = await API.getDocumentConfigs(params);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to fetch document configurations');
        // Return full response so DocumentConfigManagement can access pagination
        return response.data;
    },
    createDocumentConfig: async (data: any) => {
        const response: any = await API.createDocumentConfig(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to create document configuration');
        return response.data?.data || response.data;
    },
    updateDocumentConfig: async (id: string, data: any) => {
        const response: any = await API.updateDocumentConfig(id, data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to update document configuration');
        return response.data?.data || response.data;
    },
    deleteDocumentConfig: async (id: string) => {
        const response: any = await API.deleteDocumentConfig(id);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to delete document configuration');
        return response.data;
    },
    updateApplication: async (id: string, data: any) => {
        const response: any = await API.updateApplication(id, data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to update application');
        return response.data;
    },
    submitFddReport: async (data: any) => {
        const response: any = await API.submitFddReport(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to submit FDD report');
        return response.data;
    },
    getFddAssignment: async (applicationId: string) => {
        const response: any = await API.getFddAssignment(applicationId);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to fetch FDD assignment');
        return response.data;
    },
    assignFddAgency: async (data: any) => {
        const response: any = await API.assignFddAgency(data);
        if (!response.ok) throw new Error(response.data?.message || 'Failed to assign FDD agency');
        return response.data;
    },
    exportResponses: async (applicationIds: string[]) => {
        const response: any = await (API as any).exportApplicationResponses({ applicationIds: applicationIds.join(',') });
        if (!response.ok) throw new Error(response.data?.message || 'Failed to export responses');
        return response.data?.data || [];
    }
};
