<?php

namespace App\Controllers;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Models\ImageCleanup;
use App\Utils\Response as ApiResponse;

class ImageCleanupController
{
    /**
     * 手动触发清理冗余图片（需要管理员权限）
     */
    public function cleanupImages(Request $request, Response $response)
    {
        try {
            $model = new ImageCleanup();
            $deletedCount = $model->cleanupRedundantImages();

            $message = "成功清理 {$deletedCount} 个冗余图片";
            $response->getBody()->write(ApiResponse::json(ApiResponse::success($message)));
            return $response->withHeader('Content-Type', 'application/json; charset=utf-8');
        } catch (\Exception $e) {
            $response->getBody()->write(ApiResponse::json(ApiResponse::error('清理失败: ' . $e->getMessage())));
            return $response->withHeader('Content-Type', 'application/json; charset=utf-8')->withStatus(500);
        }
    }
}
