import { Request, Response } from 'express';
import * as userService from '../services/userService';

export const getUsers = async (req: Request, res: Response) => {
    try {
        const users = await userService.getAllUsers();
        res.json(users);
    } catch (error: any) {
        res.status(500).json({ error: error.message || 'Failed to get users' });
    }
};

export const getUser = async (req: Request, res: Response) => {
    try {
        const id = parseInt(req.params.id, 10);
        const user = await userService.getUserById(id);
        res.json(user);
    } catch (error: any) {
        res.status(404).json({ error: error.message || 'User not found' });
    }
};

export const updateUser = async (req: Request, res: Response) => {
    try {
        const id = parseInt(req.params.id, 10);
        const user = await userService.updateUser(id, req.body);
        res.json(user);
    } catch (error: any) {
        res.status(400).json({ error: error.message || 'Failed to update user' });
    }
};

export const deleteUser = async (req: Request, res: Response) => {
    try {
        const id = parseInt(req.params.id, 10);
        const result = await userService.deleteUser(id);
        res.json(result);
    } catch (error: any) {
        res.status(404).json({ error: error.message || 'User not found' });
    }
};
