<?php

namespace FleekDash\Routes\Groups;

use FleekDash\Libs\API\Route;
use FleekDash\Libs\API\RouteAuthMiddleware;
use FleekDash\Controllers\CommentController;
use FleekDash\Controllers\UserController;
use FleekDash\Controllers\NotificationsController;

/**
 * Interaction Routes
 *
 * API routes for user interactions:
 * - Comments (CRUD operations)
 * - Users (management)
 * - Notifications
 *
 * @package FleekDash\Routes\Groups
 * @since 2.2.0
 */
class InteractionRoutes
{
    private const ROUTE_COMMENTS_ID = '/comments/{id}';
    private const ROUTE_USERS_ID = '/users/{id}';
    private const DESC_COMMENT_ID = 'Comment ID';
    private const DESC_USER_ID = 'User ID';
    private const DESC_USER_EMAIL = 'User email address';
    private const DESC_USER_PASSWORD = 'User password';

    /**
     * Register all interaction-related routes
     *
     * @param Route $route Route instance
     * @return void
     */
    public static function register(Route $route)
    {
        self::registerCommentRoutes($route);
        self::registerUserRoutes($route);
        self::registerAuthRoutes($route);
    }

    /**
     * Register comment CRUD and auxiliary routes
     *
     * @param Route $route
     */
    private static function registerCommentRoutes(Route $route): void
    {
        $route->get('/comments', [CommentController::class, 'index'], RouteAuthMiddleware::authenticated(), [
            'page' => [
                'required' => false,
                'default' => 1,
                'type' => 'integer',
                'description' => 'Current page number',
                'minimum' => 1,
            ],
            'per_page' => [
                'required' => false,
                'default' => 10,
                'type' => 'integer',
                'description' => 'Number of items per page',
                'minimum' => 1,
                'maximum' => 100,
            ],
            'post_id' => [
                'required' => false,
                'type' => 'integer',
                'description' => 'Filter comments by post ID',
            ],
            'search' => [
                'required' => false,
                'type' => 'string',
                'description' => 'Search term',
            ],
            'orderby' => [
                'required' => false,
                'default' => 'date',
                'type' => 'string',
                'description' => 'Order by field',
                'enum' => ['date', 'comment_date_gmt', 'comment_ID', 'author', 'comment_approved'],
            ],
            'order' => [
                'required' => false,
                'default' => 'DESC',
                'type' => 'string',
                'description' => 'Order direction',
                'enum' => ['ASC', 'DESC', 'asc', 'desc'],
            ],
            'status' => [
                'required' => false,
                'default' => 'approve',
                'type' => 'string',
                'description' => 'Comment status',
                'enum' => ['approve', 'approved', 'hold', 'pending', 'spam', 'trash', 'any', 'all'],
            ],
        ]);

        $route->get(self::ROUTE_COMMENTS_ID, [CommentController::class, 'show'], RouteAuthMiddleware::authenticated(), [
            'id' => [
                'required' => true,
                'type' => 'integer',
                'description' => self::DESC_COMMENT_ID,
                'minimum' => 1,
            ],
        ]);

        $route->post('/comments', [CommentController::class, 'store'], RouteAuthMiddleware::authenticated(), [
            'post_id' => [
                'required' => true,
                'type' => 'integer',
                'description' => 'Post ID to comment on',
                'minimum' => 1,
            ],
            'content' => [
                'required' => true,
                'type' => 'string',
                'description' => 'Comment content',
                'minLength' => 1,
            ],
            'author_name' => [
                'required' => false,
                'type' => 'string',
                'description' => 'Comment author name (for guest comments)',
            ],
            'author_email' => [
                'required' => false,
                'type' => 'string',
                'format' => 'email',
                'description' => 'Comment author email (for guest comments)',
            ],
            'author_url' => [
                'required' => false,
                'type' => 'string',
                'format' => 'uri',
                'description' => 'Comment author website (for guest comments)',
            ],
            'parent' => [
                'required' => false,
                'type' => 'integer',
                'description' => 'Parent comment ID for replies',
                'default' => 0,
            ],
        ]);

        $route->put(self::ROUTE_COMMENTS_ID, [CommentController::class, 'update'], RouteAuthMiddleware::authenticated(), [
            'id' => [
                'required' => true,
                'type' => 'integer',
                'description' => self::DESC_COMMENT_ID,
                'minimum' => 1,
            ],
            'content' => [
                'required' => false,
                'type' => 'string',
                'description' => 'Comment content',
            ],
            'date' => [
                'required' => false,
                'type' => 'string',
                'description' => 'Comment date (MySQL format: Y-m-d H:i:s or RFC3339)',
            ],
            'status' => [
                'required' => false,
                'type' => 'string',
                'description' => 'Comment status',
                'enum' => ['approve', 'approved', 'hold', 'pending', 'spam', 'trash'],
            ],
        ]);

        $route->delete(self::ROUTE_COMMENTS_ID, [CommentController::class, 'destroy'], RouteAuthMiddleware::authenticated(), [
            'id' => [
                'required' => true,
                'type' => 'integer',
                'description' => self::DESC_COMMENT_ID,
                'minimum' => 1,
            ],
            'force' => [
                'required' => false,
                'type' => 'boolean',
                'default' => false,
                'description' => 'Whether to permanently delete the comment',
            ],
        ]);

        $route->get('/comments/stats', [CommentController::class, 'getStats'], RouteAuthMiddleware::public());
        $route->get('/comments/metadata', [CommentController::class, 'getMetadata'], RouteAuthMiddleware::public());
    }

    /**
     * Register user CRUD and profile routes
     *
     * @param Route $route
     */
    private static function registerUserRoutes(Route $route): void
    {
        self::registerUserListAndStoreRoutes($route);
        self::registerUserUpdateAndAuxRoutes($route);
    }

    /**
     * Register user list, show, me, and store routes
     *
     * @param Route $route
     */
    private static function registerUserListAndStoreRoutes(Route $route): void
    {
        $route->get('/users', [UserController::class, 'index'], RouteAuthMiddleware::authenticated(), [
            'page' => [
                'required' => false,
                'default' => 1,
                'type' => 'integer',
                'description' => 'Current page number',
                'minimum' => 1,
            ],
            'per_page' => [
                'required' => false,
                'default' => 10,
                'type' => 'integer',
                'description' => 'Number of items per page',
                'minimum' => 1,
                'maximum' => 100,
            ],
            'search' => [
                'required' => false,
                'type' => 'string',
                'description' => 'Search term',
            ],
            'orderby' => [
                'required' => false,
                'default' => 'ID',
                'type' => 'string',
                'description' => 'Order by field',
                'enum' => ['ID', 'display_name', 'user_login', 'user_email', 'user_registered'],
            ],
            'order' => [
                'required' => false,
                'default' => 'ASC',
                'type' => 'string',
                'description' => 'Order direction',
                'enum' => ['ASC', 'DESC', 'asc', 'desc'],
            ],
            'role' => [
                'required' => false,
                'type' => 'string',
                'description' => 'Filter users by role',
            ],
        ]);

        $route->get(self::ROUTE_USERS_ID, [UserController::class, 'show'], RouteAuthMiddleware::authenticated(), [
            'id' => [
                'required' => true,
                'type' => 'integer',
                'description' => self::DESC_USER_ID,
                'minimum' => 1,
            ],
        ]);

        $route->get('/users/me', [UserController::class, 'me'], RouteAuthMiddleware::authenticated());

        $route->post('/users', [UserController::class, 'store'], RouteAuthMiddleware::admin(), [
            'username' => [
                'required' => true,
                'type' => 'string',
                'description' => 'User login name',
                'minLength' => 3,
            ],
            'email' => [
                'required' => true,
                'type' => 'string',
                'format' => 'email',
                'description' => self::DESC_USER_EMAIL,
            ],
            'password' => [
                'required' => true,
                'type' => 'string',
                'description' => self::DESC_USER_PASSWORD,
                'minLength' => 6,
            ],
            'first_name' => [
                'required' => false,
                'type' => 'string',
                'description' => 'User first name',
            ],
            'last_name' => [
                'required' => false,
                'type' => 'string',
                'description' => 'User last name',
            ],
            'role' => [
                'required' => false,
                'type' => 'string',
                'description' => 'User role',
                'default' => 'subscriber',
            ],
        ]);
    }

    /**
     * Register user update, delete, roles, avatar, and locale routes
     *
     * @param Route $route
     */
    private static function registerUserUpdateAndAuxRoutes(Route $route): void
    {
        $route->put(self::ROUTE_USERS_ID, [UserController::class, 'update'], RouteAuthMiddleware::authenticated(), [
            'id' => [
                'required' => true,
                'type' => 'integer',
                'description' => self::DESC_USER_ID,
                'minimum' => 1,
            ],
            'email' => [
                'required' => false,
                'type' => 'string',
                'format' => 'email',
                'description' => self::DESC_USER_EMAIL,
            ],
            'password' => [
                'required' => false,
                'type' => 'string',
                'description' => self::DESC_USER_PASSWORD,
                'minLength' => 6,
            ],
            'first_name' => [
                'required' => false,
                'type' => 'string',
                'description' => 'User first name',
            ],
            'last_name' => [
                'required' => false,
                'type' => 'string',
                'description' => 'User last name',
            ],
            'display_name' => [
                'required' => false,
                'type' => 'string',
                'description' => 'User display name',
            ],
            'role' => [
                'required' => false,
                'type' => 'string',
                'description' => 'User role',
            ],
            'registered_date' => [
                'required' => false,
                'type' => 'string',
                'description' => 'User registration date (MySQL format: YYYY-MM-DD HH:MM:SS)',
                'pattern' => '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$',
            ],
        ]);

        $route->delete(self::ROUTE_USERS_ID, [UserController::class, 'destroy'], RouteAuthMiddleware::admin(), [
            'id' => [
                'required' => true,
                'type' => 'integer',
                'description' => self::DESC_USER_ID,
                'minimum' => 1,
            ],
            'reassign' => [
                'required' => false,
                'type' => 'integer',
                'description' => 'User ID to reassign content to',
                'default' => 0,
            ],
        ]);

        $route->get('/users/roles', [UserController::class, 'getRoles'], RouteAuthMiddleware::authenticated());

        $route->put(self::ROUTE_USERS_ID . '/roles', [UserController::class, 'updateRoles'], RouteAuthMiddleware::admin(), [
            'id' => [
                'required' => true,
                'type' => 'integer',
                'description' => self::DESC_USER_ID,
                'minimum' => 1,
            ],
            'roles' => [
                'required' => true,
                'type' => 'array',
                'items' => [
                    'type' => 'string',
                ],
                'description' => 'Array of role slugs to assign to the user',
            ],
        ]);

        $route->post(self::ROUTE_USERS_ID . '/avatar', [UserController::class, 'uploadAvatar'], RouteAuthMiddleware::authenticated(), [
            'id' => [
                'required' => true,
                'type' => 'integer',
                'description' => self::DESC_USER_ID,
                'minimum' => 1,
            ],
        ]);

        $route->post('/user/locale', [UserController::class, 'updateUserLocale'], RouteAuthMiddleware::authenticated(), [
            'locale' => [
                'required' => true,
                'type' => 'string',
                'description' => 'User locale code (e.g., en_US, pt_BR)',
                'pattern' => '^[a-z]{2}_[A-Z]{2}$',
            ],
        ]);
    }

    /**
     * Register auth routes (login, signup, forgot-password)
     *
     * @param Route $route
     */
    private static function registerAuthRoutes(Route $route): void
    {
        $route->post('/login', [UserController::class, 'login'], RouteAuthMiddleware::public(), [
            'username' => [
                'required' => true,
                'type' => 'string',
                'description' => 'User login name or email',
            ],
            'password' => [
                'required' => true,
                'type' => 'string',
                'description' => self::DESC_USER_PASSWORD,
            ],
            'remember' => [
                'required' => false,
                'type' => 'boolean',
                'description' => 'Remember me',
                'default' => false,
            ],
        ]);

        $route->post('/signup', [UserController::class, 'signup'], RouteAuthMiddleware::public(), [
            'email' => [
                'required' => true,
                'type' => 'string',
                'description' => self::DESC_USER_EMAIL,
            ],
            'username' => [
                'required' => true,
                'type' => 'string',
                'description' => 'Username',
            ],
            'password' => [
                'required' => true,
                'type' => 'string',
                'description' => self::DESC_USER_PASSWORD,
            ],
        ]);

        $route->post('/forgot-password', [UserController::class, 'forgotPassword'], RouteAuthMiddleware::public(), [
            'email' => [
                'required' => true,
                'type' => 'string',
                'description' => self::DESC_USER_EMAIL,
            ],
        ]);
    }
}
