"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceModel = void 0;
exports.parseFeatures = parseFeatures;
exports.slugify = slugify;
const db_1 = require("../utils/db");
function parseFeatures(json) {
    try {
        return JSON.parse(json) || [];
    }
    catch {
        return [];
    }
}
function slugify(name) {
    return name.toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_|_$/g, "");
}
exports.ServiceModel = {
    getAll() {
        return (0, db_1.getDb)()
            .prepare("SELECT * FROM services ORDER BY category_slug, sort_order, id")
            .all();
    },
    getByCategory(categorySlug) {
        return (0, db_1.getDb)()
            .prepare("SELECT * FROM services WHERE category_slug = ? AND is_active = 1 ORDER BY sort_order, id")
            .all(categorySlug);
    },
    getAllByCategory(categorySlug) {
        return (0, db_1.getDb)()
            .prepare("SELECT * FROM services WHERE category_slug = ? ORDER BY sort_order, id")
            .all(categorySlug);
    },
    getById(id) {
        return ((0, db_1.getDb)().prepare("SELECT * FROM services WHERE id = ?").get(id) ?? null);
    },
    getBySlug(slug) {
        return ((0, db_1.getDb)().prepare("SELECT * FROM services WHERE slug = ?").get(slug) ?? null);
    },
    create(data) {
        const n = (0, db_1.getDb)()
            .prepare("SELECT COUNT(*) as n FROM services WHERE category_slug = ?")
            .get(data.categorySlug).n;
        const r = (0, db_1.getDb)()
            .prepare("INSERT INTO services (category_slug, slug, icon, name, description, price, features, sort_order) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
            .run(data.categorySlug, data.slug, data.icon, data.name, data.description, data.price, data.features, 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 (fields.price !== undefined) {
            sets.push("price = ?");
            vals.push(fields.price);
        }
        if (fields.features !== undefined) {
            sets.push("features = ?");
            vals.push(fields.features);
        }
        if (fields.isActive !== undefined) {
            sets.push("is_active = ?");
            vals.push(fields.isActive);
        }
        if (!sets.length)
            return false;
        vals.push(id);
        return (0, db_1.getDb)().prepare(`UPDATE services SET ${sets.join(", ")} WHERE id = ?`).run(...vals).changes > 0;
    },
    delete(id) {
        return (0, db_1.getDb)().prepare("DELETE FROM services WHERE id = ?").run(id).changes > 0;
    },
    count() {
        return (0, db_1.getDb)().prepare("SELECT COUNT(*) as n FROM services").get().n;
    },
};
