<?php

declare(strict_types=1);

namespace Kylin\Prompt\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Kylin\Core\ApiResponse;
use Kylin\Core\Controllers\BaseController;
use Kylin\Prompt\Services\AIPromptCompliance;
use Kylin\Prompt\Services\PromptInternationalization;
use Kylin\Prompt\Services\PromptService;

/**
 * 提示文案管理控制器
 *
 * 提供适龄提示语查询、AI 标注、国际化等 HTTP 接口。
 *
 * 法律合规：
 *  - 《未成年人保护法》第七十五条：适龄提示按年龄分组
 *  - 《深度合成服务管理规定》第六条：AI 生成内容需标识
 *  - T/CADPA 14-2021：网络游戏适龄提示标准
 */
class PromptController extends BaseController
{
    public function __construct(private readonly PromptService $service) {}

    /**
     * 根据年龄组获取提示语
     */
    public function show(Request $request): JsonResponse
    {
        $data = $request->validate([
            'age_group' => 'required|integer|in:0,1,2,3',
            'scene' => 'required|string|in:login,time_limit,duration_warning,payment_limit,kick,health_advice,guardian_bind,face_verify,report_channel',
            'locale' => 'nullable|string|in:zh,en,ja,ko',
        ]);

        $prompt = $this->service->getPrompt((int) $data['age_group'], $data['scene']);

        // 国际化翻译
        if (!empty($data['locale']) && $data['locale'] !== 'zh') {
            $prompt = PromptInternationalization::translate($prompt, $data['locale']);
        }

        return ApiResponse::success($prompt);
    }

    /**
     * 根据实际年龄获取登录提示语
     */
    public function byAge(Request $request): JsonResponse
    {
        $data = $request->validate(['age' => 'required|integer|min:0|max:150']);
        return ApiResponse::success($this->service->getAgePrompt((int) $data['age']));
    }

    /**
     * 获取所有年龄分组的登录提示语
     */
    public function all(): JsonResponse
    {
        return ApiResponse::success($this->service->getAllPrompts());
    }

    /**
     * 获取某年龄组所有场景提示语
     */
    public function byAgeGroup(Request $request): JsonResponse
    {
        $data = $request->validate(['age_group' => 'required|integer|in:0,1,2,3']);
        return ApiResponse::success($this->service->getPromptsByAgeGroup((int) $data['age_group']));
    }

    /**
     * AI 合规标注
     */
    public function markAI(Request $request): JsonResponse
    {
        $data = $request->validate([
            'title' => 'required|string|max:128',
            'content' => 'required|string',
            'type' => 'nullable|string|in:modal,toast,banner',
        ]);

        $marked = AIPromptCompliance::markAIGenerated($data);
        return ApiResponse::success($marked, '已标注 AI 生成标识');
    }

    /**
     * AI 合规声明
     */
    public function complianceNotice(): JsonResponse
    {
        return ApiResponse::success([
            'notice' => AIPromptCompliance::complianceNotice(),
            'supported_locales' => PromptInternationalization::supportedLocales(),
        ]);
    }

    /**
     * 清除提示语缓存（管理员）
     */
    public function clearCache(): JsonResponse
    {
        $this->service->clearCache();
        return ApiResponse::success(null, '缓存已清除');
    }
}
