import express from 'express';
import { list, remove, create, update } from '../services/trims.js';

let router = express.Router();

router.get('/', async function (req, res) {
    try {
        const results = await list(req.query);
        res.json({ ok: true, results });
    } catch (error) {
        console.error('Error fetching car brands:', error);
        res.status(500).json({ ok: false, error: 'Failed to fetch car brands' });
    }
});

router.post('/', async function (req, res) {
    try {
        const status = await create(req.body);
        res.json({ ok: true, status });
    } catch (error) {
        console.error('Error creating car brand:', error);
        res.status(500).json({ ok: false, error: 'Failed to create car brand' });
    }
});

router.delete('/', async function (req, res) {
    try {
        const deletedCount = await remove(req.body);
        res.json({ ok: true, deletedCount });
    } catch (error) {
        console.error('Error deleting car brand:', error);
        res.status(500).json({ ok: false, error: 'Failed to delete car brand' });
    }
});

router.put('/', async function (req, res) {
    try {
        const updatedCount = await update(req.body);
        res.json({ ok: true, updatedCount });
    } catch (error) {
        console.error('Error updating car brand:', error);
        res.status(500).json({ ok: false, error: 'Failed to update car brand' });
    }
});

export default router;