"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.categoryRouter = void 0;
const express_1 = require("express");
const index_1 = require("../index");
const auth_1 = require("../middleware/auth");
const router = (0, express_1.Router)();
const categoryRouter = (0, express_1.Router)();
exports.categoryRouter = categoryRouter;
// ===================== TREATMENTS =====================
// GET /api/treatments - Public
router.get('/', async (req, res) => {
    try {
        const treatments = await index_1.prisma.treatment.findMany({ where: { isActive: true }, orderBy: { createdAt: 'desc' } });
        res.json(treatments);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengambil treatment' });
    }
});
// GET /api/treatments/:slug - Public
router.get('/:slug', async (req, res) => {
    try {
        const treatment = await index_1.prisma.treatment.findUnique({ where: { slug: req.params.slug } });
        if (!treatment)
            return res.status(404).json({ message: 'Treatment tidak ditemukan' });
        res.json(treatment);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengambil treatment' });
    }
});
// POST /api/treatments - Admin
router.post('/', auth_1.authMiddleware, async (req, res) => {
    try {
        const { name, slug, category, description, benefits, isActive } = req.body;
        if (!name || !slug || !category || !description)
            return res.status(400).json({ message: 'Name, slug, category, description diperlukan' });
        const treatment = await index_1.prisma.treatment.create({
            data: { name, slug, category, description, benefits: Array.isArray(benefits) ? benefits.join('||') : benefits, isActive }
        });
        res.status(201).json(treatment);
    }
    catch (error) {
        if (error.code === 'P2002')
            return res.status(400).json({ message: 'Slug sudah ada' });
        res.status(500).json({ message: 'Gagal membuat treatment' });
    }
});
// PUT /api/treatments/:id - Admin
router.put('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        const data = { ...req.body };
        if (Array.isArray(data.benefits))
            data.benefits = data.benefits.join('||');
        const treatment = await index_1.prisma.treatment.update({ where: { id: parseInt(req.params.id) }, data });
        res.json(treatment);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengupdate treatment' });
    }
});
// DELETE /api/treatments/:id - Admin
router.delete('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        await index_1.prisma.treatment.delete({ where: { id: parseInt(req.params.id) } });
        res.json({ message: 'Treatment berhasil dihapus' });
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal menghapus treatment' });
    }
});
// ===================== TREATMENT CATEGORIES (via /api/treatment-categories) =====================
// GET /api/treatment-categories - Public
categoryRouter.get('/', async (req, res) => {
    try {
        const categories = await index_1.prisma.treatmentCategory.findMany({ orderBy: { name: 'asc' } });
        res.json(categories);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengambil kategori' });
    }
});
// POST /api/treatment-categories - Admin
categoryRouter.post('/', auth_1.authMiddleware, async (req, res) => {
    try {
        const { name, slug, description } = req.body;
        if (!name || !slug)
            return res.status(400).json({ message: 'Nama dan slug diperlukan' });
        const cat = await index_1.prisma.treatmentCategory.create({ data: { name, slug, description } });
        res.status(201).json(cat);
    }
    catch (error) {
        if (error.code === 'P2002')
            return res.status(400).json({ message: 'Slug sudah ada' });
        res.status(500).json({ message: 'Gagal membuat kategori' });
    }
});
// PUT /api/treatment-categories/:id - Admin
categoryRouter.put('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        const cat = await index_1.prisma.treatmentCategory.update({ where: { id: parseInt(req.params.id) }, data: req.body });
        res.json(cat);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengupdate kategori' });
    }
});
// DELETE /api/treatment-categories/:id - Admin
categoryRouter.delete('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        await index_1.prisma.treatmentCategory.delete({ where: { id: parseInt(req.params.id) } });
        res.json({ message: 'Kategori berhasil dihapus' });
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal menghapus kategori' });
    }
});
exports.default = router;
//# sourceMappingURL=treatments.js.map