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

class ApprovedHealthCareEmail {
    static async findAll() {
        const query = `
            SELECT id, health_care_name, email, created_at
            FROM approved_health_care_emails
            ORDER BY health_care_name ASC
        `;

        try {
            const [rows] = await db.execute(query);
            return rows;
        } catch (error) {
            throw error;
        }
    }

    static async findById(id) {
        const query = `
            SELECT id, health_care_name, email, created_at
            FROM approved_health_care_emails
            WHERE id = ?
        `;

        try {
            const [rows] = await db.execute(query, [id]);
            return rows.length > 0 ? rows[0] : null;
        } catch (error) {
            throw error;
        }
    }

    static async create(healthCareData) {
        const {
            health_care_name,
            email
        } = healthCareData;

        const query = `
            INSERT INTO approved_health_care_emails (health_care_name, email)
            VALUES (?, ?)
        `;

        try {
            const [result] = await db.execute(query, [
                health_care_name,
                email
            ]);
            return result.insertId;
        } catch (error) {
            throw error;
        }
    }

    static async delete(id) {
        const query = `
            DELETE FROM approved_health_care_emails
            WHERE id = ?
        `;

        try {
            const [result] = await db.execute(query, [id]);
            return result.affectedRows > 0;
        } catch (error) {
            throw error;
        }
    }
}

module.exports = ApprovedHealthCareEmail;
