import { and, asc, eq } from "drizzle-orm";
import type { Request, Response } from "express";
import { Router } from "express";
import { db } from "../../../../pkg/db/db.js";
import { forkliftCompanies } from "../../../../pkg/db/schema/forkliftLeaseCompanys.js";
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";

const router = Router();

router.get("/", async (req: Request, res: Response) => {
	const name = req.query.name;

	const conditions = [];

	if (name !== undefined) {
		conditions.push(eq(forkliftCompanies.name, `${name}`));
	}

	//conditions.push(eq(forkliftCompanies.active, true));

	const { data, error } = await tryCatch(
		db
			.select()
			.from(forkliftCompanies)
			.where(and(...conditions))
			.orderBy(asc(forkliftCompanies.name)),
	);

	if (error) {
		return res.status(400).json({ error: error });
	}
	res.status(200).json({ message: "Current Active Companies", data: data });
});

export default router;
