"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/doctors - Public: Get all active doctors
router.get('/', async (req, res) => {
    try {
        const doctors = await index_1.prisma.doctor.findMany({
            where: { isActive: true },
            orderBy: { sortOrder: 'asc' },
        });
        res.json(doctors);
    }
    catch (error) {
        console.error('Error fetching doctors:', error);
        res.status(500).json({ message: 'Gagal mengambil data dokter' });
    }
});
// GET /api/doctors/featured - Public: Get featured doctors (limit)
router.get('/featured', async (req, res) => {
    try {
        const limit = parseInt(req.query.limit) || 4;
        const doctors = await index_1.prisma.doctor.findMany({
            where: { isActive: true },
            orderBy: { sortOrder: 'asc' },
            take: limit,
        });
        res.json(doctors);
    }
    catch (error) {
        console.error('Error fetching featured doctors:', error);
        res.status(500).json({ message: 'Gagal mengambil data dokter unggulan' });
    }
});
// GET /api/doctors/:slug - Public: Get single doctor by slug
router.get('/:slug', async (req, res) => {
    try {
        const doctor = await index_1.prisma.doctor.findUnique({
            where: { slug: req.params.slug },
        });
        if (!doctor) {
            return res.status(404).json({ message: 'Dokter tidak ditemukan' });
        }
        res.json(doctor);
    }
    catch (error) {
        console.error('Error fetching doctor:', error);
        res.status(500).json({ message: 'Gagal mengambil data dokter' });
    }
});
// POST /api/doctors - Admin: Create new doctor
router.post('/', auth_1.authMiddleware, async (req, res) => {
    try {
        const { name, slug, title, specialty, bio, photo, education, experience, services, isActive, sortOrder } = req.body;
        if (!name || !slug) {
            return res.status(400).json({ message: 'Nama dan slug diperlukan' });
        }
        // Check if slug already exists
        const existing = await index_1.prisma.doctor.findUnique({ where: { slug } });
        if (existing) {
            return res.status(400).json({ message: 'Slug sudah digunakan' });
        }
        const doctor = await index_1.prisma.doctor.create({
            data: {
                name,
                slug,
                title,
                specialty,
                bio,
                photo,
                education,
                experience,
                services,
                isActive: isActive !== undefined ? isActive : true,
                sortOrder: sortOrder || 0,
            },
        });
        res.status(201).json(doctor);
    }
    catch (error) {
        console.error('Error creating doctor:', error);
        if (error.code === 'P2002') {
            return res.status(400).json({ message: 'Slug sudah digunakan' });
        }
        res.status(500).json({ message: 'Gagal membuat dokter' });
    }
});
// PUT /api/doctors/:id - Admin: Update doctor
router.put('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        const data = { ...req.body };
        delete data.id;
        delete data.createdAt;
        delete data.updatedAt;
        // Generate slug from name if name changed and no slug provided
        if (data.name && !data.slug) {
            data.slug = data.name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '');
        }
        const doctor = await index_1.prisma.doctor.update({
            where: { id: parseInt(req.params.id) },
            data,
        });
        res.json(doctor);
    }
    catch (error) {
        console.error('Error updating doctor:', error);
        if (error.code === 'P2002') {
            return res.status(400).json({ message: 'Slug sudah digunakan' });
        }
        res.status(500).json({ message: 'Gagal mengupdate dokter' });
    }
});
// DELETE /api/doctors/:id - Admin: Delete doctor
router.delete('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        await index_1.prisma.doctor.delete({ where: { id: parseInt(req.params.id) } });
        res.json({ message: 'Dokter berhasil dihapus' });
    }
    catch (error) {
        console.error('Error deleting doctor:', error);
        res.status(500).json({ message: 'Gagal menghapus dokter' });
    }
});
exports.default = router;
//# sourceMappingURL=doctors.js.map