const API = {
    getBasePath() {
        const path = window.location.pathname;
        const match = path.match(/^(\/[^\/]+)\//);
        return match ? match[1] : '';
    },
    
    async request(url, options = {}) {
        const basePath = this.getBasePath();
        const fullUrl = basePath + url;
        
        const defaultOptions = {
            headers: {
                'Content-Type': 'application/json',
            },
        };
        
        const mergedOptions = {
            ...defaultOptions,
            ...options,
            headers: {
                ...defaultOptions.headers,
                ...options.headers,
            },
        };
        
        try {
            const response = await fetch(fullUrl, mergedOptions);
            const data = await response.json();
            
            if (!response.ok) {
                throw new Error(data.message || 'Request failed');
            }
            
            return data;
        } catch (error) {
            console.error('API Error:', error);
            throw error;
        }
    },
    
    async get(url) {
        return this.request(url, { method: 'GET' });
    },
    
    async post(url, data) {
        return this.request(url, {
            method: 'POST',
            body: JSON.stringify(data),
        });
    },
    
    async put(url, data) {
        return this.request(url, {
            method: 'PUT',
            body: JSON.stringify(data),
        });
    },
    
    async delete(url) {
        return this.request(url, { method: 'DELETE' });
    },
    
    async login(email, password) {
        return this.post('/login', { email, password });
    },
    
    getRedirectUrl(path) {
        return this.getBasePath() + path;
    },
    
    companies: {
        async create(data) {
            return API.post('/api/companies', data);
        },
        async update(id, data) {
            return API.put(`/api/companies/${id}`, data);
        },
        async delete(id) {
            return API.delete(`/api/companies/${id}`);
        },
        async toggle(id) {
            return API.post(`/api/companies/${id}/toggle`);
        },
    },
    
    staff: {
        async create(data) {
            return API.post('/api/staff', data);
        },
        async update(id, data) {
            return API.put(`/api/staff/${id}`, data);
        },
        async delete(id) {
            return API.delete(`/api/staff/${id}`);
        },
    },
    
    customers: {
        async create(data) {
            return API.post('/api/customers', data);
        },
        async update(id, data) {
            return API.put(`/api/customers/${id}`, data);
        },
        async delete(id) {
            return API.delete(`/api/customers/${id}`);
        },
    },
    
    domains: {
        async create(data) {
            return API.post('/api/domains', data);
        },
        async update(id, data) {
            return API.put(`/api/domains/${id}`, data);
        },
        async delete(id) {
            return API.delete(`/api/domains/${id}`);
        },
    },
    
    services: {
        async create(data) {
            return API.post('/api/services', data);
        },
        async update(id, data) {
            return API.put(`/api/services/${id}`, data);
        },
        async delete(id) {
            return API.delete(`/api/services/${id}`);
        },
    },
    
    reminders: {
        async create(data) {
            return API.post('/api/reminders', data);
        },
        async update(id, data) {
            return API.put(`/api/reminders/${id}`, data);
        },
        async delete(id) {
            return API.delete(`/api/reminders/${id}`);
        },
        async sendWhatsApp(id) {
            return API.post(`/api/reminders/${id}/send-whatsapp`);
        },
        async logCall(id, notes) {
            return API.post(`/api/reminders/${id}/log-call`, { notes });
        },
        async markSent(id) {
            return API.post(`/api/reminders/${id}/mark-sent`);
        },
    },
    
    async getStats() {
        return this.get('/api/stats');
    },
    
    async getDashboardData() {
        return this.get('/api/dashboard-data');
    },
    
    async lookupVehicle(registrationNumber) {
        return this.post('/customer-portal/lookup', { registration_number: registrationNumber });
    },
};
