const Ambulance = require('../models/Ambulance');

const ambulanceController = {
    // Get all ambulances
    getAll: (req, res) => {
        Ambulance.findAll((err, results) => {
            if (err) {
                return res.status(500).json({ error: err.message });
            }
            res.json(results);
        });
    },

    // Get ambulance by ID
    getById: (req, res) => {
        const ambulanceId = req.params.id;
        Ambulance.findById(ambulanceId, (err, results) => {
            if (err) {
                return res.status(500).json({ error: err.message });
            }
            if (results.length === 0) {
                return res.status(404).json({ message: 'Ambulance not found' });
            }
            res.json(results[0]);
        });
    },

    // Get ambulances by status
    getByStatus: (req, res) => {
        const status = req.params.status;
        Ambulance.findByStatus(status, (err, results) => {
            if (err) {
                return res.status(500).json({ error: err.message });
            }
            res.json(results);
        });
    },

    // Create new ambulance
    create: (req, res) => {
        const { vehicle_number, license_plate, model, make, status, latitude, longitude } = req.body;
        
        if (!vehicle_number || !license_plate) {
            return res.status(400).json({ error: 'Vehicle number and license plate are required' });
        }

        Ambulance.create({
            vehicle_number,
            license_plate,
            model,
            make,
            status,
            latitude,
            longitude
        }, (err, results) => {
            if (err) {
                // Check for duplicate entry error
                if (err.code === 'ER_DUP_ENTRY') {
                    return res.status(400).json({ error: 'Vehicle number or license plate already exists' });
                }
                return res.status(500).json({ error: err.message });
            }
            res.status(201).json({ 
                message: 'Ambulance created successfully', 
                id: results.insertId 
            });
        });
    },

    // Update ambulance
    update: (req, res) => {
        const ambulanceId = req.params.id;
        const { vehicle_number, license_plate, model, make, status, latitude, longitude } = req.body;
        
        if (!vehicle_number || !license_plate) {
            return res.status(400).json({ error: 'Vehicle number and license plate are required' });
        }

        Ambulance.update(ambulanceId, {
            vehicle_number,
            license_plate,
            model,
            make,
            status,
            latitude,
            longitude
        }, (err, results) => {
            if (err) {
                // Check for duplicate entry error
                if (err.code === 'ER_DUP_ENTRY') {
                    return res.status(400).json({ error: 'Vehicle number or license plate already exists' });
                }
                return res.status(500).json({ error: err.message });
            }
            if (results.affectedRows === 0) {
                return res.status(404).json({ message: 'Ambulance not found' });
            }
            
            // Fetch the updated ambulance and return it
            Ambulance.findById(ambulanceId, (err, ambulanceData) => {
                if (err) {
                    return res.status(500).json({ error: err.message });
                }
                if (ambulanceData.length === 0) {
                    return res.status(404).json({ message: 'Ambulance not found' });
                }
                res.json(ambulanceData[0]);
            });
        });
    },

    // Update ambulance status
    updateStatus: (req, res) => {
        const ambulanceId = req.params.id;
        const { status } = req.body;
        
        if (!status) {
            return res.status(400).json({ error: 'Status is required' });
        }

        Ambulance.updateStatus(ambulanceId, status, (err, results) => {
            if (err) {
                return res.status(500).json({ error: err.message });
            }
            if (results.affectedRows === 0) {
                return res.status(404).json({ message: 'Ambulance not found' });
            }
            res.json({ message: 'Ambulance status updated successfully' });
        });
    },

    // Update ambulance location
    updateLocation: (req, res) => {
        const ambulanceId = req.params.id;
        const { latitude, longitude } = req.body;
        
        if (latitude === undefined && longitude === undefined) {
            return res.status(400).json({ error: 'At least latitude or longitude is required' });
        }

        Ambulance.updateLocation(ambulanceId, latitude, longitude, (err, results) => {
            if (err) {
                return res.status(500).json({ error: err.message });
            }
            if (results.affectedRows === 0) {
                return res.status(404).json({ message: 'Ambulance not found' });
            }
            res.json({ message: 'Ambulance location updated successfully' });
        });
    },

    // Delete ambulance
    delete: (req, res) => {
        const ambulanceId = req.params.id;
        Ambulance.delete(ambulanceId, (err, results) => {
            if (err) {
                return res.status(500).json({ error: err.message });
            }
            if (results.affectedRows === 0) {
                return res.status(404).json({ message: 'Ambulance not found' });
            }
            res.json({ message: 'Ambulance deleted successfully' });
        });
    }
};

module.exports = ambulanceController;

