import express = require('express');
const unitsRouter = express.Router();

import pool from '../db';
import { requireLogin } from '../middleware/auth';
import { logger } from '../services/logging/logger';
import { Unit } from '@app/shared/types/units';

unitsRouter.use(requireLogin);

//get all units
unitsRouter.get('/', async (req, res) => {
    try {
        const result: Unit[] = await pool.query('SELECT * FROM units WHERE active = 1;');
        res.json(result);
    } catch (error) {
        logger.error(
            'app',
            'Failed to get all units',
            {
                error: error instanceof Error ? error.message : String(error),
                stack: error instanceof Error ? error.stack : undefined,
            }
        );
        res.sendStatus(500);
    }
});

export const units = unitsRouter;
