<?php

declare(strict_types=1);

namespace Kylin\Report\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Kylin\Core\ApiResponse;
use Kylin\Core\Controllers\BaseController;
use Kylin\Report\Services\ApprovalMaterialService;
use Kylin\Report\Services\TestAccountService;

/**
 * 版号申报材料控制器
 *
 * 提供版号申报材料生成、测试账号查询等 HTTP 接口。
 *
 * 法律合规：
 *  - 《关于防止未成年人沉迷网络游戏的通知》（国新出发〔2019〕34号）
 *  - 《网络游戏管理暂行办法》
 */
class ReportController extends BaseController
{
    public function __construct(
        private readonly ApprovalMaterialService $approvalService,
        private readonly TestAccountService $testAccountService,
    ) {}

    /**
     * 生成防沉迷系统说明书
     */
    public function systemManual(Request $request): JsonResponse
    {
        $data = $request->validate(['game_id' => 'required|integer|min:1']);

        try {
            $markdown = $this->approvalService->generateSystemManual((int) $data['game_id']);
            return ApiResponse::success(['content' => $markdown, 'format' => 'markdown']);
        } catch (\RuntimeException $e) {
            return ApiResponse::error($e->getMessage(), 404);
        }
    }

    /**
     * 生成检测报告
     */
    public function testReport(Request $request): JsonResponse
    {
        $data = $request->validate(['game_id' => 'required|integer|min:1']);
        return ApiResponse::success($this->approvalService->generateTestReport((int) $data['game_id']));
    }

    /**
     * 获取测试账号清单
     */
    public function testAccounts(Request $request): JsonResponse
    {
        $data = $request->validate(['game_id' => 'required|integer|min:1']);
        return ApiResponse::success($this->testAccountService->getTestAccounts((int) $data['game_id']));
    }
}
