const { getDatabase } = require('../config/database');

const WarehouseLocation = {
    getAll() {
        const db = getDatabase();
        return db.prepare(`SELECT * FROM warehouse_locations ORDER BY name`).all();
    },

    getById(id) {
        const db = getDatabase();
        return db.prepare(`SELECT * FROM warehouse_locations WHERE id = ?`).get(id);
    },

    create(data) {
        const db = getDatabase();
        const result = db.prepare(
            `INSERT INTO warehouse_locations (name, description) VALUES (@name, @description)`
        ).run({ name: data.name, description: data.description || '' });
        return this.getById(result.lastInsertRowid);
    },

    update(id, data) {
        const db = getDatabase();
        db.prepare(
            `UPDATE warehouse_locations SET name = @name, description = @description WHERE id = @id`
        ).run({ id, name: data.name, description: data.description || '' });
        return this.getById(id);
    },

    delete(id) {
        const db = getDatabase();
        const result = db.prepare(`DELETE FROM warehouse_locations WHERE id = ?`).run(id);
        return result.changes > 0;
    },
};

module.exports = WarehouseLocation;
