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

const Ambulance = {
    // Get all ambulances
    findAll: (callback) => {
        const query = 'SELECT * FROM ambulance ORDER BY id DESC';
        db.query(query, callback);
    },

    // Get ambulance by ID
    findById: (ambulanceId, callback) => {
        const query = 'SELECT * FROM ambulance WHERE id = ?';
        db.query(query, [ambulanceId], callback);
    },

    // Get ambulance by vehicle number
    findByVehicleNumber: (vehicleNumber, callback) => {
        const query = 'SELECT * FROM ambulance WHERE vehicle_number = ?';
        db.query(query, [vehicleNumber], callback);
    },

    // Get ambulance by license plate
    findByLicensePlate: (licensePlate, callback) => {
        const query = 'SELECT * FROM ambulance WHERE license_plate = ?';
        db.query(query, [licensePlate], callback);
    },

    // Get ambulances by status
    findByStatus: (status, callback) => {
        const query = 'SELECT * FROM ambulance WHERE status = ? ORDER BY id DESC';
        db.query(query, [status], callback);
    },

    // Create new ambulance
    create: (ambulanceData, callback) => {
        const {
            vehicle_number,
            license_plate,
            model,
            make,
            status,
            latitude,
            longitude
        } = ambulanceData;

        const query = 
            `INSERT INTO ambulance 
            (vehicle_number, license_plate, model, make, status, latitude, longitude) 
            VALUES (?, ?, ?, ?, ?, ?, ?)`;
        
        db.query(query, [
            vehicle_number,
            license_plate,
            model || null,
            make || null,
            status || 'available',
            latitude || null,
            longitude || null
        ], callback);
    },

    // Update ambulance
    update: (ambulanceId, ambulanceData, callback) => {
        const {
            vehicle_number,
            license_plate,
            model,
            make,
            status,
            latitude,
            longitude
        } = ambulanceData;

        const query = 
            `UPDATE ambulance 
            SET vehicle_number = ?, license_plate = ?, model = ?, make = ?, status = ?, latitude = ?, longitude = ? 
            WHERE id = ?`;
        
        db.query(query, [
            vehicle_number,
            license_plate,
            model,
            make,
            status,
            latitude || null,
            longitude || null,
            ambulanceId
        ], callback);
    },

    // Update ambulance status
    updateStatus: (ambulanceId, status, callback) => {
        const query = 'UPDATE ambulance SET status = ? WHERE id = ?';
        db.query(query, [status, ambulanceId], callback);
    },

    // Update ambulance location
    updateLocation: (ambulanceId, latitude, longitude, callback) => {
        const query = 'UPDATE ambulance SET latitude = ?, longitude = ? WHERE id = ?';
        db.query(query, [latitude || null, longitude || null, ambulanceId], callback);
    },

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

module.exports = Ambulance;

