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

const Driver = {
    // Get all drivers
    findAll: (callback) => {
        const query = 
            `SELECT d.*, u.first_name, u.last_name, u.email, u.phone_number 
            FROM driver d 
            JOIN users u ON d.user_id = u.id 
            ORDER BY d.id DESC`;
        db.query(query, callback);
    },

    // Get driver by ID
    findById: (driverId, callback) => {
        const query = 
            `SELECT d.*, u.first_name, u.last_name, u.email, u.phone_number 
            FROM driver d 
            JOIN users u ON d.user_id = u.id 
            WHERE d.id = ?`;
        db.query(query, [driverId], callback);
    },

    // Get driver by user ID
    findByUserId: (userId, callback) => {
        const query = 
            `SELECT d.*, u.first_name, u.last_name, u.email, u.phone_number 
            FROM driver d 
            JOIN users u ON d.user_id = u.id 
            WHERE d.user_id = ?`;
        db.query(query, [userId], callback);
    },

    // Get driver by license number
    findByLicenseNumber: (licenseNumber, callback) => {
        const query = 
            `SELECT d.*, u.first_name, u.last_name, u.email, u.phone_number 
            FROM driver d 
            JOIN users u ON d.user_id = u.id 
            WHERE d.license_number = ?`;
        db.query(query, [licenseNumber], callback);
    },

    // Get drivers by status
    findByStatus: (status, callback) => {
        const query = 
            `SELECT d.*, u.first_name, u.last_name, u.email, u.phone_number 
            FROM driver d 
            JOIN users u ON d.user_id = u.id 
            WHERE d.status = ? 
            ORDER BY d.id DESC`;
        db.query(query, [status], callback);
    },

    // Get drivers by ambulance ID
    findByAmbulanceId: (ambulanceId, callback) => {
        const query = 
            `SELECT d.*, u.first_name, u.last_name, u.email, u.phone_number 
            FROM driver d 
            JOIN users u ON d.user_id = u.id 
            WHERE d.ambulance_id = ? 
            ORDER BY d.id DESC`;
        db.query(query, [ambulanceId], callback);
    },

    // Get driver with assigned ambulance and user details by user ID
    findDetailedByUserId: (userId, callback) => {
        const query = `
            SELECT 
                d.id AS driver_id,
                d.user_id,
                d.license_number,
                d.ambulance_id AS assigned_ambulance_id,
                d.status AS driver_status,
                d.created_at AS driver_created_at,
                d.updated_at AS driver_updated_at,
                u.first_name,
                u.last_name,
                u.email,
                u.phone_number,
                u.role,
                u.is_verified,
                a.id AS ambulance_id,
                a.vehicle_number,
                a.license_plate,
                a.model,
                a.make,
                a.status AS ambulance_status,
                a.created_at AS ambulance_created_at,
                a.updated_at AS ambulance_updated_at
            FROM driver d
            JOIN users u ON d.user_id = u.id
            LEFT JOIN ambulance a ON d.ambulance_id = a.id
            WHERE d.user_id = ?
        `;
        db.query(query, [userId], callback);
    },

    // Create new driver
    create: (driverData, callback) => {
        const {
            user_id,
            license_number,
            ambulance_id,
            status
        } = driverData;

        const query = 
            `INSERT INTO driver 
            (user_id, license_number, ambulance_id, status) 
            VALUES (?, ?, ?, ?)`;
        
        db.query(query, [
            user_id,
            license_number,
            ambulance_id || null,
            status || 'on-duty'
        ], callback);
    },

    // Update driver
    update: (driverId, driverData, callback) => {
        const {
            license_number,
            ambulance_id,
            status
        } = driverData;
        console.log(driverData);
        const query = 
            `UPDATE driver 
            SET license_number = ?, ambulance_id = ?, status = ? 
            WHERE id = ?`;
        
        db.query(query, [
            license_number,
            ambulance_id,
            status,
            driverId
        ], callback);
    },

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

    // Assign driver to ambulance
    assignAmbulance: (driverId, ambulanceId, callback) => {
        const query = 'UPDATE driver SET ambulance_id = ? WHERE id = ?';
        db.query(query, [ambulanceId, driverId], callback);
    },

    // Unassign driver from ambulance
    unassignAmbulance: (driverId, callback) => {
        const query = 'UPDATE driver SET ambulance_id = NULL WHERE id = ?';
        db.query(query, [driverId], callback);
    },

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

    // Partially update driver by user ID (only provided fields)
    updateByUserIdPartial: (userId, driverData, callback) => {
        const { license_number, ambulance_id, status } = driverData;

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

        if (license_number !== undefined) {
            updates.push('license_number = ?');
            values.push(license_number);
        }
        if (ambulance_id !== undefined) {
            updates.push('ambulance_id = ?');
            values.push(ambulance_id);
        }
        if (status !== undefined) {
            updates.push('status = ?');
            values.push(status);
        }

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

        values.push(userId);
        const query = `UPDATE driver SET ${updates.join(', ')} WHERE user_id = ?`;
        db.query(query, values, callback);
    }
};

module.exports = Driver;


