import api from './api';

const assetService = {
    /**
     * Get all assets
     */
    getAll: async () => {
        const response = await api.get('/assets');
        return response.data.data;
    },

    /**
     * Get asset by ID
     */
    getById: async (id) => {
        const response = await api.get(`/assets/${id}`);
        return response.data.data;
    },

    /**
     * Get asset by serial number
     */
    getBySerial: async (serialNumber) => {
        const response = await api.get(`/assets/serial/${serialNumber}`);
        return response.data.data;
    },

    /**
     * Get assets by status
     */
    getByStatus: async (status) => {
        const response = await api.get(`/assets/status/${status}`);
        return response.data.data;
    },

    /**
     * Get assets by type
     */
    getByType: async (type) => {
        const response = await api.get(`/assets/type/${type}`);
        return response.data.data;
    },

    /**
     * Create new asset
     */
    create: async (assetData) => {
        const response = await api.post('/assets', assetData);
        return response.data.data;
    },

    /**
     * Update asset
     */
    update: async (id, assetData) => {
        const response = await api.put(`/assets/${id}`, assetData);
        return response.data.data;
    },

    /**
     * Delete asset
     */
    delete: async (id) => {
        const response = await api.delete(`/assets/${id}`);
        return response.data;
    },

    /**
     * Assign asset to user
     */
    assign: async (id, userId, notes) => {
        const response = await api.post(`/assets/${id}/assign`, {
            user_id: userId,
            notes: notes
        });
        return response.data.data;
    },

    /**
     * Unassign asset from user
     */
    unassign: async (id, newStatus) => {
        const response = await api.post(`/assets/${id}/unassign`, {
            new_status: newStatus
        });
        return response.data.data;
    },

    /**
     * Get assignment history for an asset
     */
    getAssignmentHistory: async (id) => {
        const response = await api.get(`/assets/${id}/history`);
        return response.data.data;
    },

    /**
     * Get statistics
     */
    getStatistics: async () => {
        const response = await api.get('/assets/stats');
        return response.data.data;
    },

    /**
     * Open printable label PDF in new tab
     */
    printLabel: async (id, copies = 1, size = 'medium') => {
        const response = await api.get(`/assets/${id}/label`, {
            params: { copies, size },
            responseType: 'blob',
        });
        const blob = new Blob([response.data], { type: 'application/pdf' });
        const url = URL.createObjectURL(blob);
        window.open(url, '_blank');
        setTimeout(() => URL.revokeObjectURL(url), 60000);
    },

    /**
     * Import managed devices from Microsoft Intune
     * Returns { imported, skipped, errors }
     */
    importFromIntune: async () => {
        const response = await api.post('/assets/import/intune');
        return response.data.data;
    },

    /**
     * Get all inspections for an asset
     */
    getInspections: (assetId) => api.get(`/assets/${assetId}/inspections`).then(r => r.data.data),

    /**
     * Create an inspection for an asset
     */
    createInspection: (assetId, data) => api.post(`/assets/${assetId}/inspections`, data).then(r => r.data.data),

    /**
     * Open handover protocol PDF in new tab
     */
    printHandoverProtocol: async (id) => {
        const response = await api.get(`/assets/${id}/handover-protocol`, {
            responseType: 'blob',
        });
        const blob = new Blob([response.data], { type: 'application/pdf' });
        const url = URL.createObjectURL(blob);
        window.open(url, '_blank');
        setTimeout(() => URL.revokeObjectURL(url), 60000);
    },

    // Asset Types
    getTypes: async () => {
        const response = await api.get('/asset-types');
        return response.data;
    },
    createType: async (data) => {
        const response = await api.post('/asset-types', data);
        return response.data;
    },
    updateType: async (id, data) => {
        const response = await api.put(`/asset-types/${id}`, data);
        return response.data;
    },
    deleteType: async (id) => {
        await api.delete(`/asset-types/${id}`);
    },
    syncFromAgent: async (id) => {
        const response = await api.post(`/assets/${id}/sync-agent`);
        return response.data;
    },
};

export default assetService;
