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

export const masterService = {
    // Roles & Permissions
    getRoles: async () => {
        const response = await API.getRoles();
        return response.data;
    },
    getPermissions: async () => {
        const response = await API.getPermissions();
        return response.data;
    },
    updateRole: async (id: string, data: any) => {
        const response = await API.updateRole(id, data);
        return response.data;
    },
    createRole: async (data: any) => {
        const response = await (API as any).createRole(data);
        return response.data;
    },

    // Zones & Regions
    getZones: async () => {
        const response = await API.getZones();
        return response.data;
    },
    updateZone: async (id: string, data: any) => {
        const response = await API.updateZone(id, data);
        return response.data;
    },
    createZone: async (data: any) => {
        const response = await API.createZone(data);
        return response.data;
    },
    createRegion: async (data: any) => {
        const response = await API.createRegion(data);
        return response.data;
    },
    updateRegion: async (id: string, data: any) => {
        const response = await API.updateRegion(id, data);
        return response.data;
    },
    getRegions: async () => {
        const response = await API.getRegions();
        return response.data;
    },
    getStates: async (params?: any) => {
        const response = await API.getStates(params);
        return response.data;
    },
    getDistricts: async (params?: any) => {
        const queryParams = typeof params === 'string' ? { stateId: params, limit: 'all' } : { limit: 'all', ...params };
        const response = await API.getDistricts(queryParams);
        return response.data;
    },
    getAreas: async (params?: any) => {
        const response = await API.getAreas(params);
        return response.data;
    },
    updateArea: async (id: string, data: any) => {
        const response = await API.updateArea(id, data);
        return response.data;
    },
    createArea: async (data: any) => {
        const response = await API.createArea(data);
        return response.data;
    },
    deleteArea: async (id: string) => {
        const response = await (API as any).deleteArea(id);
        return response.data;
    },
    getAreaManagers: async () => {
        const response = await API.getAreaManagers();
        return response.data;
    },
    getASMs: async () => {
        const response = await (API as any).getASMs();
        return response.data;
    },
    getZonalManagers: async () => {
        const response = await (API as any).getZonalManagers();
        return response.data;
    },
    saveZonalManager: async (data: any) => {
        return (API as any).saveZonalManager(data).then((res: any) => res.data);
    },
    getDDLeads: async () => {
        const response = await (API as any).getDDLeads();
        return response.data;
    },
    saveDDLead: async (data: any) => {
        return (API as any).saveDDLead(data).then((res: any) => res.data);
    },


    // User Management
    getUsers: async () => {
        const response = await API.getUsers();
        return response.data;
    },
    updateUser: async (id: string, data: any) => {
        const response = await API.updateUser(id, data);
        return response.data;
    },

    // Email Templates
    getEmailTemplates: async () => {
        const response = await API.getEmailTemplates();
        return response.data;
    },
    getEmailTemplate: async (id: string) => {
        const response = await API.getEmailTemplate(id);
        return response.data;
    },
    createEmailTemplate: async (data: any) => {
        const response = await API.createEmailTemplate(data);
        return response.data;
    },
    updateEmailTemplate: async (id: string, data: any) => {
        const response = await API.updateEmailTemplate(id, data);
        return response.data;
    },
    deleteEmailTemplate: async (id: string) => {
        const response = await API.deleteEmailTemplate(id);
        return response.data;
    },
    previewEmailTemplate: async (data: any) => {
        const response = await API.previewEmailTemplate(data);
        return response.data;
    },
    
    // SLA
    getSlaConfigs: async () => {
        const response = await API.getSlaConfigs();
        return response.data;
    },
    saveSlaConfig: async (data: any) => {
        const response = await API.saveSlaConfig(data);
        return response.data;
    },
    initializeDefaultSlas: async () => {
        const response = await (API as any).initializeDefaultSlas();
        return response.data;
    },

    // Semantic wrappers for consistent API
    saveZone: async (data: any) => {
        return data.id ? masterService.updateZone(data.id, data) : masterService.createZone(data);
    },
    saveRegion: async (data: any) => {
        return data.id ? masterService.updateRegion(data.id, data) : masterService.createRegion(data);
    },
    saveASM: async (data: any) => {
        // ASMs are handled via user update in this system
        return API.updateUser(data.userId, data).then(res => res.data);
    },
    // System Configs
    getSystemConfigs: async (params?: any) => {
        const response = await API.getSystemConfigs(params);
        return response.data;
    },
    saveSystemConfig: async (data: any) => {
        const response = await API.saveSystemConfig(data);
        return response.data;
    },
    getDealerAsmMappings: async () => {
        const response = await (API as any).getDealerAsmMappings();
        return response.data;
    },
    saveDealerAsmMapping: async (data: { dealerId: string; asmUserId?: string | null }) => {
        const response = await (API as any).saveDealerAsmMapping(data);
        return response.data;
    }
};
