/**
 * ==================================================
 * Smart Attendance System
 * Device Model
 * ==================================================
 * Handles database operations for attendance devices.
 * ==================================================
 */

const db = require("../config/db");

class Device {

    // ==========================================
    // Create Device
    // ==========================================

    static async create(data) {

        const [result] = await db.execute(

            `
            INSERT INTO devices (

                device_id,
                device_name,
                device_type,
                serial_number,
                location,
                room,
                ip_address,
                mac_address,
                firmware_version,
                api_key,
                status

            )

            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            `,

            [

                data.device_id,
                data.device_name,
                data.device_type,
                data.serial_number,
                data.location,
                data.room,
                data.ip_address,
                data.mac_address,
                data.firmware_version,
                data.api_key,
                data.status || "OFFLINE"

            ]

        );

        return result.insertId;

    }

    // ==========================================
    // Find All
    // ==========================================

    static async findAll() {

        const [rows] = await db.execute(

            `
            SELECT *
            FROM devices
            ORDER BY device_name ASC
            `

        );

        return rows;

    }

    // ==========================================
    // Find By ID
    // ==========================================

    static async findById(id) {

        const [rows] = await db.execute(

            `
            SELECT *
            FROM devices
            WHERE id = ?
            LIMIT 1
            `,

            [id]

        );

        return rows[0] || null;

    }

    // ==========================================
    // Find By Device ID
    // ==========================================

    static async findByDeviceId(deviceId) {

        const [rows] = await db.execute(

            `
            SELECT *
            FROM devices
            WHERE device_id = ?
            LIMIT 1
            `,

            [deviceId]

        );

        return rows[0] || null;

    }

    // ==========================================
    // Update Device
    // ==========================================

    static async update(id, data) {

        const [result] = await db.execute(

            `
            UPDATE devices

            SET

                device_name = ?,
                device_type = ?,
                serial_number = ?,
                location = ?,
                room = ?,
                ip_address = ?,
                mac_address = ?,
                firmware_version = ?,
                status = ?

            WHERE id = ?
            `,

            [

                data.device_name,
                data.device_type,
                data.serial_number,
                data.location,
                data.room,
                data.ip_address,
                data.mac_address,
                data.firmware_version,
                data.status,
                id

            ]

        );

        return result.affectedRows;

    }

    // ==========================================
    // Update Last Seen
    // ==========================================

    static async updateLastSeen(id, ipAddress = null) {

        const [result] = await db.execute(

            `
            UPDATE devices

            SET

                last_seen = CURRENT_TIMESTAMP,
                ip_address = COALESCE(?, ip_address),
                status = 'ONLINE'

            WHERE id = ?
            `,

            [

                ipAddress,
                id

            ]

        );

        return result.affectedRows;

    }

    // ==========================================
    // Delete Device
    // ==========================================

    static async delete(id) {

        const [result] = await db.execute(

            `
            DELETE FROM devices
            WHERE id = ?
            `,

            [id]

        );

        return result.affectedRows;

    }

}

module.exports = Device;