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

class HistoryHomesteadService {
    getHistoryHomestead(homestead_id) {
        return new Promise((res, rej) => {
            let sql = `
                SELECT 
                    *
                FROM 
                    homestead_history
                WHERE 
                    homestead_history.homestead_id = '${homestead_id}'
                ORDER BY 
                    homestead_history.date_start
            `;

            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),
                            "name": row.name,
                            "group_id": Number(row.group_id),
                            "sheep_id": Number(row.sheep_id),
                            "working": Number(row.working) == 0 ? false : true,
                            "date": {
                                "start": Number(row.date_start),
                                "end": row.date_end ? Number(row.date_end) : null
                            }
                        }
                    })

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

    createHistoryHomestead(homestead_id, data) {
        return new Promise((res, rej) => {
            let sql = 'INSERT INTO homestead_history(homestead_id, name, date_start, group_id, sheep_id, working) VALUES (?, ?, ?, ?, ?, ?)';

            db.run(sql, [
                homestead_id,
                data.name,
                Math.floor(new Date(Date.now()).getTime()),
                Number(data.group_id),
                Number(data.sheep_id),
                1
            ], function (err) {
                if (err) {
                    console.error(err.message);
                    return res(false);
                } else if (this.changes === 0) {
                    return res(false);
                } else {
                    if (Number(data.sheep_id) > 0) {
                        Notification.sendSheep({
                            sheep_id: Number(data.sheep_id),
                            title: "Нова територія",
                            body: "Вам призначено нову територію"
                        });
                    } else if (Number(data.sheep_id) == 0 && Number(data.group_id) > 0) {
                        Notification.sendGroup({
                            group_id: Number(data.group_id),
                            title: "Нова територія",
                            body: "Призначено нову групову територію"
                        });
                    }

                    res({ "create": "ok", "id": this.lastID });
                }
            });
        });
    }

    updateHistoryHomestead(homestead_id) {
        return new Promise((res, rej) => {
            let sql = 'UPDATE homestead_history SET date_end = ?, working = ? WHERE id = ?';
            db.run(sql, [
                Math.floor(new Date(Date.now()).getTime()),
                0,
                Number(homestead_id)
            ], function (err) {
                if (err) {
                    console.error(err.message);
                    return res(false);
                } else if (this.changes === 0) {
                    return res(false);
                } else {
                    res({ "update": "ok", "id": homestead_id });
                }
            });
        });
    }

    deleteHistoryHomestead(data) {
        return new Promise((res, rej) => {
            db.run('DELETE FROM homestead_history WHERE id = ?', [Number(homestead_id)], function (err) {
                if (err) {
                    console.error(err.message);
                    return res(false);
                } else if (this.changes === 0) {
                    return res(false);
                } else {
                    res({ "delete": "ok", "id": homestead_id });
                }
            });
        });
    }
}

module.exports = new HistoryHomesteadService();