"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/locations - Public: Get all locations
router.get('/', async (req, res) => {
    try {
        const locations = await index_1.prisma.location.findMany({
            orderBy: { city: 'asc' }
        });
        res.json(locations);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengambil lokasi', error: error instanceof Error ? error.message : '' });
    }
});
// GET /api/locations/:id - Public: Get single location
router.get('/:id', async (req, res) => {
    try {
        const location = await index_1.prisma.location.findUnique({
            where: { id: parseInt(req.params.id) }
        });
        if (!location) {
            return res.status(404).json({ message: 'Lokasi tidak ditemukan' });
        }
        res.json(location);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengambil lokasi', error: error instanceof Error ? error.message : '' });
    }
});
// GET /api/locations/city/:city - Public: Get locations by city
router.get('/city/:city', async (req, res) => {
    try {
        const locations = await index_1.prisma.location.findMany({
            where: { city: req.params.city }
        });
        res.json(locations);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengambil lokasi', error: error instanceof Error ? error.message : '' });
    }
});
// POST /api/locations - Admin: Create new location
router.post('/', auth_1.authMiddleware, async (req, res) => {
    try {
        const { name, address, city, phone, whatsapp, latitude, longitude, type } = req.body;
        if (!name || !address || !city) {
            return res.status(400).json({ message: 'Name, address, dan city diperlukan' });
        }
        const location = await index_1.prisma.location.create({
            data: {
                name,
                address,
                city,
                phone,
                whatsapp,
                latitude: latitude ? parseFloat(latitude) : undefined,
                longitude: longitude ? parseFloat(longitude) : undefined,
                type: type || 'Skin Clinic'
            }
        });
        res.status(201).json(location);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal membuat lokasi', error: error instanceof Error ? error.message : '' });
    }
});
// PUT /api/locations/:id - Admin: Update location
router.put('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        const { latitude, longitude, ...rest } = req.body;
        const location = await index_1.prisma.location.update({
            where: { id: parseInt(req.params.id) },
            data: {
                ...rest,
                ...(latitude && { latitude: parseFloat(latitude) }),
                ...(longitude && { longitude: parseFloat(longitude) })
            }
        });
        res.json(location);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengupdate lokasi', error: error instanceof Error ? error.message : '' });
    }
});
// DELETE /api/locations/:id - Admin: Delete location
router.delete('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        await index_1.prisma.location.delete({ where: { id: parseInt(req.params.id) } });
        res.json({ message: 'Lokasi berhasil dihapus' });
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal menghapus lokasi', error: error instanceof Error ? error.message : '' });
    }
});
exports.default = router;
//# sourceMappingURL=locations.js.map