"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const multer_1 = __importDefault(require("multer"));
const path_1 = __importDefault(require("path"));
const index_1 = require("../index");
const auth_1 = require("../middleware/auth");
const router = (0, express_1.Router)();
const storage = multer_1.default.diskStorage({
    destination: (req, file, cb) => cb(null, 'uploads/banners'),
    filename: (req, file, cb) => {
        const ext = path_1.default.extname(file.originalname);
        cb(null, `banner-${Date.now()}${ext}`);
    },
});
const upload = (0, multer_1.default)({ storage, limits: { fileSize: 5 * 1024 * 1024 }, fileFilter: (req, file, cb) => {
        cb(null, /jpeg|jpg|png|gif|webp/.test(path_1.default.extname(file.originalname).toLowerCase()));
    } });
// GET /api/banners - Public
router.get('/', async (req, res) => {
    try {
        const banners = await index_1.prisma.banner.findMany({ orderBy: { sortOrder: 'asc' } });
        res.json(banners);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengambil banner' });
    }
});
// POST /api/banners - Admin
router.post('/', auth_1.authMiddleware, upload.single('image'), async (req, res) => {
    try {
        const { title, subtitle, link, buttonText, sortOrder, isActive } = req.body;
        if (!title)
            return res.status(400).json({ message: 'Judul diperlukan' });
        const image = req.file ? `/uploads/banners/${req.file.filename}` : undefined;
        const banner = await index_1.prisma.banner.create({
            data: { title, subtitle, link, buttonText, image, sortOrder: sortOrder ? parseInt(sortOrder) : 1, isActive: isActive === 'true' }
        });
        res.status(201).json(banner);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal membuat banner' });
    }
});
// PUT /api/banners/:id - Admin
router.put('/:id', auth_1.authMiddleware, upload.single('image'), async (req, res) => {
    try {
        const data = { ...req.body };
        if (data.sortOrder)
            data.sortOrder = parseInt(data.sortOrder);
        if (data.isActive)
            data.isActive = data.isActive === 'true';
        if (req.file)
            data.image = `/uploads/banners/${req.file.filename}`;
        const banner = await index_1.prisma.banner.update({ where: { id: parseInt(req.params.id) }, data });
        res.json(banner);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengupdate banner' });
    }
});
// DELETE /api/banners/:id - Admin
router.delete('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        await index_1.prisma.banner.delete({ where: { id: parseInt(req.params.id) } });
        res.json({ message: 'Banner berhasil dihapus' });
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal menghapus banner' });
    }
});
exports.default = router;
//# sourceMappingURL=banners.js.map