const express = require('express');
const Kitchen = require('../models/Kitchen'); // Make sure to update the path if necessary
const router = express.Router();

// Create a new kitchen
router.post('/', async (req, res) => {
    try {
        const kitchen = new Kitchen(req.body);
        await kitchen.save();
        res.status(201).send('Kitchen registered');
    } catch (error) {
        res.status(400).send(error.message);
    }
});

// Read all kitchens
router.get('/', async (req, res) => {
    try {
        const kitchens = await Kitchen.find();
        res.status(200).json(kitchens);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

// Read a single kitchen by ID
router.get('/:id', async (req, res) => {
    try {
        const kitchen = await Kitchen.findById(req.params.id);
        if (!kitchen) {
            return res.status(404).send('Kitchen not found');
        }
        res.status(200).json(kitchen);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

// Update a kitchen by ID
router.put('/:id', async (req, res) => {
    try {
        const kitchen = await Kitchen.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true });
        if (!kitchen) {
            return res.status(404).send('Kitchen not found');
        }
        res.status(200).send('Kitchen updated');
    } catch (error) {
        res.status(400).send(error.message);
    }
});

// Delete a kitchen by ID
router.delete('/:id', async (req, res) => {
    try {
        const kitchen = await Kitchen.findByIdAndDelete(req.params.id);
        if (!kitchen) {
            return res.status(404).send('Kitchen not found');
        }
        res.status(200).send('Kitchen deleted');
    } catch (error) {
        res.status(500).send(error.message);
    }
});

module.exports = router;
