<?php

use Slim\Routing\RouteCollectorProxy;
use App\Controllers\UserController;
use App\Controllers\MarkdownController;
use App\Controllers\GroupingController;
use App\Controllers\ImageController;
use App\Controllers\SystemController;
use App\Controllers\TrashController;
use App\Controllers\ImageCleanupController;
use App\Middleware\AuthMiddleware;

// 根路径测试路由
$app->get('/', function ($request, $response) {
    $data = [
        'status' => 'success',
        'message' => 'Biji PHP API is running',
        'version' => '1.0.0',
        'endpoints' => [
            'POST /api/user/register' => '用户注册',
            'POST /api/user/login' => '用户登录',
            'GET /api/markdown' => '获取所有笔记',
            'GET /api/markdown/{id}' => '获取单个笔记',
            'GET /api/groupings' => '获取分组列表'
        ]
    ];
    $response->getBody()->write(json_encode($data, JSON_UNESCAPED_UNICODE));
    return $response->withHeader('Content-Type', 'application/json');
});

// 用户相关路由（无需认证）
$app->group('/api/user', function (RouteCollectorProxy $group) {
    $group->post('/register', [UserController::class, 'register']);
    $group->post('/login', [UserController::class, 'login']);
});

// 用户相关路由（需要认证）
$app->group('/api/user', function (RouteCollectorProxy $group) {
    $group->post('/validate-token', [UserController::class, 'validateToken']);
    $group->put('/password', [UserController::class, 'updatePassword']);
    $group->delete('/deleteUser', [UserController::class, 'deleteUser']);
})->add(new AuthMiddleware());

// Markdown 相关路由（无需认证 - 公开阅读）
// 注意：静态路由必须在动态路由之前定义，避免路由冲突
$app->group('/api/markdown', function (RouteCollectorProxy $group) {
    $group->get('', [MarkdownController::class, 'getAll']);
    $group->get('/search', [MarkdownController::class, 'search']);
    $group->get('/recent', [MarkdownController::class, 'getRecent']);
    $group->get('/grouping/{groupingId}', [MarkdownController::class, 'getByGroupingId']);
    $group->get('/{id}', [MarkdownController::class, 'getById']);
});

// Markdown 相关路由（需要认证）
$app->group('/api/markdown', function (RouteCollectorProxy $group) {
    $group->post('/updateMarkdown', [MarkdownController::class, 'update']);
    $group->post('/{id}/title', [MarkdownController::class, 'updateTitle']);
    $group->delete('/{id}', [MarkdownController::class, 'delete']);
})->add(new AuthMiddleware());

// 分组相关路由（无需认证）
$app->group('/api/groupings', function (RouteCollectorProxy $group) {
    $group->get('', [GroupingController::class, 'getAll']);
});

// 分组相关路由（需要认证）
$app->group('/api/groupings', function (RouteCollectorProxy $group) {
    $group->post('', [GroupingController::class, 'create']);
    $group->put('/{id}', [GroupingController::class, 'update']);
    $group->delete('/{id}', [GroupingController::class, 'delete']);
})->add(new AuthMiddleware());

// 图片相关路由（无需认证 - 公开预览）
$app->group('/api/images', function (RouteCollectorProxy $group) {
    $group->get('/preview/{url}', [ImageController::class, 'preview']);
});

// 图片相关路由（需要认证）
// 注意：静态路由必须在动态路由之前定义，避免路由冲突
$app->group('/api/images', function (RouteCollectorProxy $group) {
    $group->post('', [ImageController::class, 'upload']);
    $group->post('/deleteByUrl', [ImageController::class, 'deleteByUrl']);
    $group->post('/batch', [ImageController::class, 'batchDelete']);
    $group->post('/{id}', [ImageController::class, 'delete']);
})->add(new AuthMiddleware());

// 系统设置相关路由（无需认证 - 公开查询注册状态）
$app->group('/api/system', function (RouteCollectorProxy $group) {
    $group->get('/registration/status', [SystemController::class, 'getRegistrationStatus']);
});

// 系统设置相关路由（需要认证）
$app->group('/api/system', function (RouteCollectorProxy $group) {
    $group->post('/registration/toggle', [SystemController::class, 'toggleRegistration']);
    $group->post('/registration/generate-code', [SystemController::class, 'generateRegistrationCode']);
})->add(new AuthMiddleware());

// 回收站相关路由（需要认证）
$app->group('/api/trash', function (RouteCollectorProxy $group) {
    $group->get('', [TrashController::class, 'getTrashItems']);
    $group->post('/restore/{type}/{id}', [TrashController::class, 'restoreItem']);
    $group->delete('/permanently/{type}/{id}', [TrashController::class, 'permanentlyDeleteItem']);
    $group->delete('/clean', [TrashController::class, 'cleanTrash']);
})->add(new AuthMiddleware());

// 管理员相关路由（需要认证 - 图片清理）
$app->group('/api/admin', function (RouteCollectorProxy $group) {
    $group->post('/cleanup-images', [ImageCleanupController::class, 'cleanupImages']);
})->add(new AuthMiddleware());
