const Car = require("../model/Car");
const generateSequence = require("../utils/generateSequence");

const createCar = async (req, res) => {
  try {
    const userId = req.session?.user?.id;
    const carNumber = await generateSequence("car", "CAR");
    const plateNumber = await generateSequence("plate", "PLT");
    const car = await Car.create({ ...req.body, userId, carNumber, plateNumber });
    return res.status(201).json(car);
  } catch (error) {
    return res.status(400).json({ message: error.message });
  }
};

const getCars = async (req, res) => {
  const userId = req.session?.user?.id;
  const cars = await Car.find({ userId }).sort({ createdAt: -1 });
  return res.json(cars);
};


module.exports = { createCar, getCars };
