const db = require('../config/database');

const IncidentDetails = {
    // Get all incident details
    findAll: (callback) => {
        const query = `
            SELECT 
                id.*,
                it.name as incident_type_name
            FROM incident_details id
            LEFT JOIN incident_type it ON id.incident_type_id = it.id
            ORDER BY id.id DESC
        `;
        db.query(query, callback);
    },

    // Get incident details by ID
    findById: (detailsId, callback) => {
        const query = `
            SELECT 
                id.*,
                it.name as incident_type_name
            FROM incident_details id
            LEFT JOIN incident_type it ON id.incident_type_id = it.id
            WHERE id.id = ?
        `;
        db.query(query, [detailsId], callback);
    },

    // Get incident details by request ID
    findByRequestId: (requestId, callback) => {
        const query = `
            SELECT 
                id.*,
                it.name as incident_type_name
            FROM incident_details id
            LEFT JOIN incident_type it ON id.incident_type_id = it.id
            WHERE id.request_id = ?
        `;
        db.query(query, [requestId], callback);
    },

    // Create new incident details
    create: (detailsData, callback) => {
        const {
            request_id,
            number_of_casualties,
            is_member,
            bill_accepted,
            incident_type_id,
            incident_requirement_notes,
            general_notes
        } = detailsData;

        const query = `
            INSERT INTO incident_details 
            (request_id, number_of_casualties, is_member, bill_accepted, incident_type_id, incident_requirement_notes, general_notes) 
            VALUES (?, ?, ?, ?, ?, ?, ?)
        `;
        
        db.query(query, [
            request_id,
            number_of_casualties || 0,
            is_member || false,
            bill_accepted || false,
            incident_type_id || null,
            incident_requirement_notes || null,
            general_notes || null
        ], callback);
    },

    // Update incident details
    update: (detailsId, detailsData, callback) => {
        const {
            number_of_casualties,
            is_member,
            bill_accepted,
            incident_type_id,
            incident_requirement_notes,
            general_notes
        } = detailsData;

        const updates = [];
        const values = [];

        if (number_of_casualties !== undefined) {
            updates.push('number_of_casualties = ?');
            values.push(number_of_casualties);
        }
        if (is_member !== undefined) {
            updates.push('is_member = ?');
            values.push(is_member);
        }
        if (bill_accepted !== undefined) {
            updates.push('bill_accepted = ?');
            values.push(bill_accepted);
        }
        if (incident_type_id !== undefined) {
            updates.push('incident_type_id = ?');
            values.push(incident_type_id);
        }
        if (incident_requirement_notes !== undefined) {
            updates.push('incident_requirement_notes = ?');
            values.push(incident_requirement_notes);
        }
        if (general_notes !== undefined) {
            updates.push('general_notes = ?');
            values.push(general_notes);
        }

        if (updates.length === 0) {
            return callback(new Error('No fields to update'), null);
        }

        values.push(detailsId);
        const query = `UPDATE incident_details SET ${updates.join(', ')} WHERE id = ?`;
        db.query(query, values, callback);
    },

    // Delete incident details
    delete: (detailsId, callback) => {
        const query = 'DELETE FROM incident_details WHERE id = ?';
        db.query(query, [detailsId], callback);
    }
};

module.exports = IncidentDetails;

