<?php

namespace App\Controllers;

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

class TrashController
{
    /**
     * 获取回收站列表
     */
    public function getTrashItems(Request $request, Response $response)
    {
        $model = new Trash();
        $items = $model->getTrashItems();

        $response->getBody()->write(ApiResponse::json(ApiResponse::success($items)));
        return $response->withHeader('Content-Type', 'application/json; charset=utf-8');
    }

    /**
     * 恢复项目
     */
    public function restoreItem(Request $request, Response $response, $args)
    {
        $type = $args['type'];
        $id = $args['id'];

        try {
            $model = new Trash();
            $model->restoreItem($id, $type);

            $response->getBody()->write(ApiResponse::json(ApiResponse::success(null, '恢复成功')));
            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);
        }
    }

    /**
     * 永久删除项目
     */
    public function permanentlyDeleteItem(Request $request, Response $response, $args)
    {
        $type = $args['type'];
        $id = $args['id'];

        try {
            $model = new Trash();
            $model->permanentlyDeleteItem($id, $type);

            $response->getBody()->write(ApiResponse::json(ApiResponse::success(null, '永久删除成功')));
            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);
        }
    }

    /**
     * 清空回收站
     */
    public function cleanTrash(Request $request, Response $response)
    {
        try {
            $model = new Trash();
            $model->cleanTrash();

            $response->getBody()->write(ApiResponse::json(ApiResponse::success(null, '清空成功')));
            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);
        }
    }
}
