const AuthService = require('../services/auth.service');
const User = require('../models/User');
const { asyncHandler } = require('../middleware/errorHandler');

class AuthController {
    /**
     * Login user
     * POST /api/auth/login
     */
    static login = asyncHandler(async (req, res) => {
        const { username, password } = req.body;

        if (!username || !password) {
            return res.status(400).json({
                status: 'error',
                message: 'Username and password are required'
            });
        }

        const result = await AuthService.login(username, password);

        res.json({
            status: 'success',
            data: result
        });
    });

    /**
     * Get current user info
     * GET /api/auth/me
     */
    static getCurrentUser = asyncHandler(async (req, res) => {
        const user = User.getById(req.user.id);

        if (!user) {
            return res.status(404).json({
                status: 'error',
                message: 'User not found'
            });
        }

        const { password_hash, ...userWithoutPassword } = user;

        res.json({
            status: 'success',
            data: userWithoutPassword
        });
    });

    /**
     * Change password
     * POST /api/auth/change-password
     */
    static changePassword = asyncHandler(async (req, res) => {
        const { currentPassword, newPassword } = req.body;

        if (!currentPassword || !newPassword) {
            return res.status(400).json({
                status: 'error',
                message: 'Current password and new password are required'
            });
        }

        const result = await AuthService.changePassword(
            req.user.id,
            currentPassword,
            newPassword
        );

        res.json({
            status: 'success',
            data: result
        });
    });

    /**
     * Update email notification preference
     * PUT /api/auth/notifications
     */
    static updateNotifications = asyncHandler(async (req, res) => {
        const { email_notifications } = req.body;
        const updated = User.updateEmailNotifications(req.user.id, !!email_notifications);
        const { password_hash, ...userWithoutPassword } = updated;
        res.json({ status: 'success', data: userWithoutPassword });
    });

    /**
     * PUT /api/auth/staff-notifications
     */
    static updateStaffNotifications = asyncHandler(async (req, res) => {
        const { notif_ticket_created, notif_ticket_assigned, notif_new_comment, notif_weekly_report } = req.body;
        const updated = User.updateStaffNotifications(req.user.id, {
            notif_ticket_created,
            notif_ticket_assigned,
            notif_new_comment,
            notif_weekly_report,
        });
        const { password_hash, ...userWithoutPassword } = updated;
        res.json({ status: 'success', data: userWithoutPassword });
    });

    /**
     * Logout user
     * POST /api/auth/logout
     */
    static logout = asyncHandler(async (req, res) => {
        // Client-side will handle token removal
        // This endpoint is just for consistency and potential future server-side session handling
        res.json({
            status: 'success',
            message: 'Logged out successfully'
        });
    });
}

module.exports = AuthController;
