"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const index_1 = require("../index");
const auth_1 = require("../middleware/auth");
const router = (0, express_1.Router)();
// GET /api/promo-categories
router.get('/', async (req, res) => {
    try {
        const cats = await index_1.prisma.promoCategory.findMany({ orderBy: { name: 'asc' } });
        res.json(cats);
    }
    catch (error) {
        console.error('Error fetching promo categories:', error);
        res.status(500).json({ message: 'Gagal mengambil kategori promo' });
    }
});
// POST /api/promo-categories
router.post('/', auth_1.authMiddleware, async (req, res) => {
    try {
        const { name, slug } = req.body;
        if (!name || !slug)
            return res.status(400).json({ message: 'Nama dan slug diperlukan' });
        const cat = await index_1.prisma.promoCategory.create({ data: { name, slug } });
        res.status(201).json({ message: 'Kategori berhasil dibuat', ...cat });
    }
    catch (error) {
        console.error('Error creating promo category:', error);
        if (error.code === 'P2002')
            return res.status(400).json({ message: 'Slug sudah ada' });
        res.status(500).json({ message: 'Gagal membuat kategori promo' });
    }
});
// PUT /api/promo-categories/:id
router.put('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        const { name, slug } = req.body;
        const cat = await index_1.prisma.promoCategory.update({
            where: { id: parseInt(req.params.id) },
            data: { name, slug }
        });
        res.json({ message: 'Kategori berhasil diupdate', ...cat });
    }
    catch (error) {
        console.error('Error updating promo category:', error);
        if (error.code === 'P2002')
            return res.status(400).json({ message: 'Slug sudah ada' });
        res.status(500).json({ message: 'Gagal mengupdate kategori promo' });
    }
});
// DELETE /api/promo-categories/:id
router.delete('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        await index_1.prisma.promoCategory.delete({ where: { id: parseInt(req.params.id) } });
        res.json({ message: 'Kategori berhasil dihapus' });
    }
    catch (error) {
        console.error('Error deleting promo category:', error);
        res.status(500).json({ message: 'Gagal menghapus kategori promo' });
    }
});
exports.default = router;
//# sourceMappingURL=promo-categories.js.map