"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceCategoryModel = void 0;
const db_1 = require("../utils/db");
exports.ServiceCategoryModel = {
    getAll() {
        return (0, db_1.getDb)()
            .prepare("SELECT * FROM service_categories ORDER BY sort_order, id")
            .all();
    },
    getBySlug(slug) {
        return ((0, db_1.getDb)()
            .prepare("SELECT * FROM service_categories WHERE slug = ?")
            .get(slug) ?? null);
    },
    getById(id) {
        return ((0, db_1.getDb)()
            .prepare("SELECT * FROM service_categories WHERE id = ?")
            .get(id) ?? null);
    },
    create(data) {
        const n = (0, db_1.getDb)().prepare("SELECT COUNT(*) as n FROM service_categories").get().n;
        const r = (0, db_1.getDb)()
            .prepare("INSERT INTO service_categories (slug, icon, name, description, sort_order) VALUES (?, ?, ?, ?, ?)")
            .run(data.slug, data.icon, data.name, data.description, n);
        return r.lastInsertRowid;
    },
    update(id, fields) {
        const sets = [];
        const vals = [];
        if (fields.icon !== undefined) {
            sets.push("icon = ?");
            vals.push(fields.icon);
        }
        if (fields.name !== undefined) {
            sets.push("name = ?");
            vals.push(fields.name);
        }
        if (fields.description !== undefined) {
            sets.push("description = ?");
            vals.push(fields.description);
        }
        if (!sets.length)
            return false;
        vals.push(id);
        return (0, db_1.getDb)().prepare(`UPDATE service_categories SET ${sets.join(", ")} WHERE id = ?`).run(...vals).changes > 0;
    },
    delete(id) {
        return (0, db_1.getDb)().prepare("DELETE FROM service_categories WHERE id = ?").run(id).changes > 0;
    },
    count() {
        return (0, db_1.getDb)().prepare("SELECT COUNT(*) as n FROM service_categories").get().n;
    },
};
