import { Router } from 'express';

import { authenticate } from '../../middlewares/auth.middleware';
import { socialAuthController } from '../social/social.controller';

import { authController } from './auth.controller';
import { changePasswordController } from './change-password.controller';

/**
 * EN: Create and configure auth routes
 * VI: Tạo và cấu hình routes cho auth
 */
export const createAuthRouter = (): Router => {
    const router = Router();

    // =============================================================================
    // EN: Authentication Endpoints
    // VI: Endpoints Xác Thực
    // =============================================================================

    /**
     * @swagger
     * /api/{version}/auth/register:
     *   post:
     *     summary: Register a new user
     *     description: Register a new user account with email and password
     *     tags: [Authentication]
     *     parameters:
     *       - in: path
     *         name: version
     *         required: true
     *         schema:
     *           type: string
     *           default: v1
     *         description: API version
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             $ref: '#/components/schemas/RegisterRequest'
     *     responses:
     *       201:
     *         description: User registered successfully
     *         content:
     *           application/json:
     *             schema:
     *               $ref: '#/components/schemas/RegisterResponse'
     *       400:
     *         description: Validation error or email already in use
     *       500:
     *         description: Internal server error
     */
    router.post('/register', authController.register.bind(authController));

    /**
     * @swagger
     * /api/{version}/auth/login:
     *   post:
     *     summary: Login user
     *     description: Authenticate user with email and password to receive access tokens
     *     tags: [Authentication]
     *     parameters:
     *       - in: path
     *         name: version
     *         required: true
     *         schema:
     *           type: string
     *           default: v1
     *         description: API version
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             $ref: '#/components/schemas/LoginRequest'
     *     responses:
     *       200:
     *         description: Login successful
     *         content:
     *           application/json:
     *             schema:
     *               $ref: '#/components/schemas/LoginResponse'
     *       401:
     *         description: Invalid credentials
     *       500:
     *         description: Internal server error
     */
    router.post('/login', authController.login.bind(authController));

    /**
     * @swagger
     * /api/{version}/auth/logout:
     *   post:
     *     summary: Logout user
     *     description: Invalidate current user session and tokens
     *     tags: [Authentication]
     *     security:
     *       - bearerAuth: []
     *     parameters:
     *       - in: path
     *         name: version
     *         required: true
     *         schema:
     *           type: string
     *           default: v1
     *         description: API version
     *     responses:
     *       200:
     *         description: Logout successful
     *         content:
     *           application/json:
     *             schema:
     *               $ref: '#/components/schemas/ApiResponse'
     *       401:
     *         description: Unauthorized
     */
    router.post('/logout', authenticate(), authController.logout.bind(authController));

    /**
     * @swagger
     * /api/{version}/auth/refresh:
     *   post:
     *     summary: Refresh access token
     *     description: Get a new access token using a valid refresh token
     *     tags: [Authentication]
     *     parameters:
     *       - in: path
     *         name: version
     *         required: true
     *         schema:
     *           type: string
     *           default: v1
     *         description: API version
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             type: object
     *             required: [refreshToken]
     *             properties:
     *               refreshToken:
     *                 type: string
     *                 description: The refresh token
     *     responses:
     *       200:
     *         description: Token refreshed successfully
     *         content:
     *           application/json:
     *             schema:
     *               $ref: '#/components/schemas/LoginResponse'
     *       401:
     *         description: Invalid or expired refresh token
     */
    router.post('/refresh', authController.refreshToken.bind(authController));

    /**
     * @swagger
     * /api/{version}/auth/change-password:
     *   post:
     *     summary: Change password
     *     description: Change the password for the currently authenticated user
     *     tags: [Authentication]
     *     security:
     *       - bearerAuth: []
     *     parameters:
     *       - in: path
     *         name: version
     *         required: true
     *         schema:
     *           type: string
     *           default: v1
     *         description: API version
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             type: object
     *             required: [currentPassword, newPassword]
     *             properties:
     *               currentPassword:
     *                 type: string
     *               newPassword:
     *                 type: string
     *     responses:
     *       200:
     *         description: Password changed successfully
     *         content:
     *           application/json:
     *             schema:
     *               $ref: '#/components/schemas/ApiResponse'
     *       400:
     *         description: Validation error or wrong password
     *       401:
     *         description: Unauthorized
     */
    router.post('/change-password', authenticate(), changePasswordController.changePassword.bind(changePasswordController));

    /**
     * @swagger
     * /api/{version}/auth/me:
     *   get:
     *     summary: Get current user
     *     description: Get information about the currently authenticated user
     *     tags: [Authentication]
     *     security:
     *       - bearerAuth: []
     *     parameters:
     *       - in: path
     *         name: version
     *         required: true
     *         schema:
     *           type: string
     *           default: v1
     *         description: API version
     *     responses:
     *       200:
     *         description: User information retrieved
     *         content:
     *           application/json:
     *             schema:
     *               allOf:
     *                 - $ref: '#/components/schemas/ApiResponse'
     *                 - type: object
     *                   properties:
     *                     data:
     *                       type: object
     *                       description: User payload from token
     *       401:
     *         description: Unauthorized
     */
    router.get('/me', authenticate(), (req, res) => {
        res.json({
            success: true,
            data: req.user,
            message: 'User information retrieved / Thông tin người dùng đã được lấy',
            timestamp: new Date().toISOString(),
        });
    });

    // =============================================================================
    // EN: Password Reset Endpoints
    // VI: Endpoints Đặt Lại Mật Khẩu
    // =============================================================================

    /**
     * @swagger
     * /api/{version}/auth/verify-email:
     *   post:
     *     summary: Verify email address
     *     description: Verify user email address with verification token
     *     tags: [Authentication]
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             type: object
     *             required: [token]
     *             properties:
     *               token:
     *                 type: string
     *     responses:
     *       200:
     *         description: Email verified successfully
     *       400:
     *         description: Invalid or expired token
     */
    router.post('/verify-email', authController.verifyEmail.bind(authController));

    /**
     * @swagger
     * /api/{version}/auth/forgot-password:
     *   post:
     *     summary: Request password reset
     *     description: Send password reset email to user
     *     tags: [Authentication]
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             type: object
     *             required: [email]
     *             properties:
     *               email:
     *                 type: string
     *     responses:
     *       200:
     *         description: Reset email sent
     *       404:
     *         description: User not found
     */
    router.post('/forgot-password', authController.forgotPassword.bind(authController));

    /**
     * @swagger
     * /api/{version}/auth/reset-password:
     *   post:
     *     summary: Reset password
     *     description: Reset user password with reset token
     *     tags: [Authentication]
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             type: object
     *             required: [token, password]
     *             properties:
     *               token:
     *                 type: string
     *               password:
     *                 type: string
     *     responses:
     *       200:
     *         description: Password reset successfully
     *       400:
     *         description: Invalid or expired token
     */
    router.post('/reset-password', authController.resetPassword.bind(authController));

    // =============================================================================
    // EN: Social Authentication Endpoints
    // VI: Endpoints Xác Thực Mạng Xã Hội
    // =============================================================================

    /**
     * @swagger
     * /api/{version}/auth/google:
     *   get:
     *     summary: Initiate Google OAuth
     *     description: Redirects to Google login page for OAuth2 authentication
     *     tags: [Authentication]
     *     parameters:
     *       - in: path
     *         name: version
     *         required: true
     *         schema:
     *           type: string
     *           default: v1
     *         description: API version
     *     responses:
     *       302:
     *         description: Redirects to Google
     */
    router.get('/google', socialAuthController.googleAuth.bind(socialAuthController));
    router.get('/google/callback', socialAuthController.googleCallback.bind(socialAuthController));

    /**
     * @swagger
     * /api/{version}/auth/facebook:
     *   get:
     *     summary: Initiate Facebook OAuth
     *     description: Redirects to Facebook login page for OAuth2 authentication
     *     tags: [Authentication]
     *     parameters:
     *       - in: path
     *         name: version
     *         required: true
     *         schema:
     *           type: string
     *           default: v1
     *         description: API version
     *     responses:
     *       302:
     *         description: Redirects to Facebook
     */
    router.get('/facebook', socialAuthController.facebookAuth.bind(socialAuthController));
    router.get('/facebook/callback', socialAuthController.facebookCallback.bind(socialAuthController));

    /**
     * @swagger
     * /api/{version}/auth/github:
     *   get:
     *     summary: Initiate GitHub OAuth
     *     description: Redirects to GitHub login page for OAuth2 authentication
     *     tags: [Authentication]
     *     parameters:
     *       - in: path
     *         name: version
     *         required: true
     *         schema:
     *           type: string
     *           default: v1
     *         description: API version
     *     responses:
     *       302:
     *         description: Redirects to GitHub
     */
    router.get('/github', socialAuthController.githubAuth.bind(socialAuthController));
    router.get('/github/callback', socialAuthController.githubCallback.bind(socialAuthController));

    return router;
};
