"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)();
// Multer storage for photo upload
const storage = multer_1.default.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'uploads/testimonials');
    },
    filename: (req, file, cb) => {
        const ext = path_1.default.extname(file.originalname);
        cb(null, `testimonial-${Date.now()}${ext}`);
    },
});
const upload = (0, multer_1.default)({
    storage,
    limits: { fileSize: 2 * 1024 * 1024 }, // 2MB
    fileFilter: (req, file, cb) => {
        const allowed = /jpeg|jpg|png|gif|webp/;
        const extOk = allowed.test(path_1.default.extname(file.originalname).toLowerCase());
        const mimeOk = allowed.test(file.mimetype.split('/')[1]);
        cb(null, extOk && mimeOk);
    },
});
// GET /api/testimonials - Public
router.get('/', async (req, res) => {
    try {
        const testimonials = await index_1.prisma.testimonial.findMany({
            where: { isActive: true },
            orderBy: { createdAt: 'desc' },
        });
        res.json(testimonials);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengambil testimonial' });
    }
});
// POST /api/testimonials - Admin (with photo upload)
router.post('/', auth_1.authMiddleware, upload.single('photo'), async (req, res) => {
    try {
        const { name, text, rating, position } = req.body;
        if (!name || !text)
            return res.status(400).json({ message: 'Nama dan teks testimonial diperlukan' });
        const photo = req.file ? `/uploads/testimonials/${req.file.filename}` : undefined;
        const slug = name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '');
        const testimonial = await index_1.prisma.testimonial.create({
            data: {
                name,
                slug,
                text,
                rating: rating ? parseInt(rating) : 5,
                photo,
                position,
                isActive: true,
            },
        });
        res.status(201).json(testimonial);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal membuat testimonial' });
    }
});
// POST /api/testimonials/upload - Admin upload photo only
router.post('/upload', auth_1.authMiddleware, upload.single('photo'), async (req, res) => {
    try {
        if (!req.file)
            return res.status(400).json({ message: 'File tidak ditemukan' });
        res.json({ photo: `/uploads/testimonials/${req.file.filename}` });
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal upload foto' });
    }
});
// PUT /api/testimonials/:id - Admin
router.put('/:id', auth_1.authMiddleware, upload.single('photo'), async (req, res) => {
    try {
        const data = { ...req.body };
        if (req.file)
            data.photo = `/uploads/testimonials/${req.file.filename}`;
        if (data.rating)
            data.rating = parseInt(data.rating);
        delete data.id;
        const testimonial = await index_1.prisma.testimonial.update({
            where: { id: parseInt(req.params.id) },
            data,
        });
        res.json(testimonial);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengupdate testimonial' });
    }
});
// DELETE /api/testimonials/:id - Admin
router.delete('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        await index_1.prisma.testimonial.delete({ where: { id: parseInt(req.params.id) } });
        res.json({ message: 'Testimonial berhasil dihapus' });
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal menghapus testimonial' });
    }
});
exports.default = router;
//# sourceMappingURL=testimonials.js.map