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


class BuildingsService {
    getList(homestead_id) {
        return new Promise((res, rej) => {
            let sql = `
                SELECT 
                    * 
                FROM 
                    buildings
                WHERE 
                    homestead_id = '${homestead_id}'
                ORDER BY 
                    id
            `;

            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),
                            "geo": JSON.parse(row.geo),
                            "title": row.title,
                            "status": Number(row.status),
                            "description": row.description,
                            "updated_at": Number(row.updated_at)
                        }       
                    })

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

    createBuildings(homestead_id, data) {
        return new Promise((res, rej) => {
            let sql = 'INSERT INTO buildings(homestead_id, title, geo) VALUES (?, ?, ?)';
            
            db.run(sql, [
                homestead_id,
                data.title,
                JSON.stringify(data.geo)
            ], function(err) {
                if (err) {
                    console.error(err.message);
                    return res(false);
                } else if (this.changes === 0) {
                    return res(false);
                } else {
                    res({ "status": "ok", "id": this.lastID });
                }
            });
        });
    }

    updateBuildings(data) {
        return new Promise((res, rej) => {
            let sql = 'UPDATE buildings SET title = ?, status = ?, description = ?, updated_at = ?, geo = ? WHERE id = ?';
            db.run(sql, [
                data.title,
                data.status,
                data.description,
                Math.floor(new Date(Date.now()).getTime()),
                JSON.stringify(data.geo),
                data.id
            ], function(err) {
                if (err) {
                    console.error(err.message);
                    return res(false);
                } else if (this.changes === 0) {
                    return res(false);
                } else {
                    res({ "status": "ok" });
                }
            });
        });
    }

    deleteBuildings(building_id) {
        return new Promise((res, rej) => {
            db.run('DELETE FROM buildings WHERE id = ?', [building_id], function(err) {
                if (err) {
                    console.error(err.message);
                    return res(false);
                } else if (this.changes === 0) {
                    return res(false);
                } else {
                    res({ "status": "ok"});
                }
            });
        });
    }
}

module.exports = new BuildingsService();