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


class ApartmentsService {
    getApartments(entrance_id) {
        return new Promise((res, rej) => {
            let sql = `
                SELECT 
                    * 
                FROM 
                    apartments
                WHERE 
                    entrance_id = '${entrance_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),
                            "entrance_id": Number(row.entrance_id),
                            "apartment_number": row.apartment_number,
                            "title": row.title,
                            "floors_number": Number(row.floors_number),
                            "status": Number(row.status),
                            "description": row.description,
                            "updated_at": Number(row.updated_at)
                        }       
                    })

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

    createApartments(entrance_id, data) {
        return new Promise((res, rej) => {
            let sql = 'INSERT INTO apartments(entrance_id, apartment_number, title, floors_number) VALUES (?, ?, ?, ?)';
            
            db.run(sql, [
                entrance_id,
                Number(data.apartment_number),
                data.title,
                data.floors_number
            ], 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 });
                }
            });
        });
    }

    updateApartments(data) {
        return new Promise((res, rej) => {
            let sql = 'UPDATE apartments SET title = ?, status = ?, description = ?, updated_at = ? WHERE id = ?';
            db.run(sql, [
                data.title,
                data.status,
                data.description,
                Math.floor(new Date(Date.now()).getTime()),
                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", "id": data.id });
                }
            });
        });
    }

    deleteApartments(data) {
        return new Promise((res, rej) => {
            db.run('DELETE FROM apartments WHERE id = ?', [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", "id": data.id });
                }
            });
        });
    }
}

module.exports = new ApartmentsService();