const db = require("../config/db");
const Notification = require("../utils/notification.js");

class HomesteadJointService {
    getList(homestead_id) {
        return new Promise((res, rej) => {
            let sql = `
                SELECT 
                    *
                FROM 
                    homestead_joint
                WHERE 
                    homestead_joint.homestead_id = '${homestead_id}'
                ORDER BY 
                    homestead_joint.created_at
            `;

            db.all(sql, (err, rows) => {
                if (err) {
                    console.error(err.message);
                    return res(false);
                } else {
                    let data = rows.map((row) => {
                        return {
                            "id": Number(row.id),
                            "homestead_id": Number(row.homestead_id),
                            "sheep_id": Number(row.sheep_id),
                            "created_at": Number(row.created_at)
                        }
                    })

                    return res(data);
                }
            });
        });
    }

    createJoint(homestead_id, data) {
        return new Promise((res, rej) => {
            let sql = 'INSERT INTO homestead_joint(homestead_id, sheep_id, created_at) VALUES (?, ?, ?)';

            db.run(sql, [
                Number(homestead_id),
                Number(data.sheep_id),
                Date.now()
            ], function (err) {
                if (err) {
                    console.error(err.message);
                    return res(false);
                } else if (this.changes === 0) {
                    return res(false);
                } else {
                    Notification.sendSheep({
                        sheep_id: Number(data.sheep_id),
                        title: "Тимчасова територія",
                        body: "Вам надали спільний доступ до території"
                    });
                    
                    res({ "create": "ok", "id": this.lastID });
                }
            });
        });
    }

    deleteJoint(homestead_id, data) {
        return new Promise((res, rej) => {
            db.run('DELETE FROM homestead_joint WHERE homestead_id = ? AND sheep_id = ?', [Number(homestead_id), Number(data.sheep_id)], function (err) {
                if (err) {
                    console.error(err.message);
                    return res(false);
                } else if (this.changes === 0) {
                    return res(false);
                } else {
                    Notification.sendSheep({
                        sheep_id: Number(data.sheep_id),
                        title: "Тимчасова територія",
                        body: "Вам відкликанно спільний доступ до території"
                    });

                    res({ "delete": "ok", "homestead_id": Number(homestead_id), "sheep_id": Number(data.sheep_id)});
                }
            });
        });
    }
}

module.exports = new HomesteadJointService();