import { AuthService } from '../services/AuthService.js';
import { safeError } from '../utils/errorHandler.js';

export class AuthController {
    static async login(req, res) {
        const { email, password } = req.body;
        if (!email || !password) {
            return res.status(400).json({ message: 'Email and password are required.' });
        }

        try {
            const result = await AuthService.login(email, password);
            res.json(result);
        } catch (error) {
            if (error.message === 'Invalid credentials') {
                return res.status(401).json({ message: 'Invalid credentials' });
            }
            safeError(res, error);
        }
    }

    static async register(req, res) {
        try {
            const result = await AuthService.createUser(req.body);
            res.status(201).json(result);
        } catch (error) {
            safeError(res, error);
        }
    }
}
