const logger = require("../../utils/logger");
const TrucksList = require("./trucks.json");
const CargoVanList = require("./cargovans.json");
const PassengerVanList = require("./passengervans.json");
const SuvList = require("./suvs.json");


const vehicletype = async (req, res) => {
    try {
        const { model } = req.body;
        if (!model || model.trim() === "") {
            res.status(400).json({ success: false, error: "Please provide a model" });
        } else {

            const type = getVehicleType(model.trim())
            res.status(200).json({ success: true, ...type });
        }
    } catch (error) {
        logger.log("vehicletype-error", "ERROR", req?.user?.email, null, {
            error: error.message,
            stack: error.stack
        });
        res.status(500).json({ error: error.message, stack: error.stack });
    }
};

function getVehicleType(model) {
    const inTrucks = TrucksList.includes(model.toUpperCase());
    const inPV = PassengerVanList.includes(model.toUpperCase());
    const inSuv = SuvList.includes(model.toUpperCase());
    const inCv = CargoVanList.includes(model.toUpperCase());

    if (inTrucks) return { type: "TK", match: true };
    else if (inPV) return { type: "PC", match: true };
    else if (inSuv) return { type: "SUV", match: true };
    else if (inCv) return { type: "VN", match: true };
    else return { type: "PC", match: false };
}
exports.default = vehicletype;
