<?php

declare(strict_types=1);

namespace Kylin\Report\Services;

use Illuminate\Support\Facades\DB;

/**
 * 版号审核测试账号服务
 * - 生成各年龄段测试账号
 * - 管理测试账号状态
 */
class TestAccountService
{
    /**
     * 获取游戏的测试账号
     * 按版署审核要求，需提供各年龄段测试账号
     *
     * @return array<int, array{id: int, name: string, id_card: string, age_group: int, age_group_label: string, phone: string, password: string}>
     */
    public function getTestAccounts(int $gameId): array
    {
        $accounts = DB::table('kr_test_accounts')
            ->where('game_id', $gameId)
            ->where('status', 1)
            ->get();

        if ($accounts->isEmpty()) {
            return $this->generateDefaultTestAccounts($gameId);
        }

        return $accounts->map(fn ($a) => [
            'id' => $a->id,
            'name' => $a->name,
            'id_card' => $a->id_card_masked,
            'age_group' => $a->age_group,
            'age_group_label' => $this->getAgeGroupLabel($a->age_group),
            'phone' => $a->phone,
            'password' => $a->password_hint,
        ])->toArray();
    }

    /**
     * 生成默认测试账号
     * 按版署审核要求，每个年龄段至少1个账号
     */
    private function generateDefaultTestAccounts(int $gameId): array
    {
        $accounts = [
            [
                'game_id' => $gameId,
                'name' => '测试幼儿',
                'id_card' => '810000201801011234',
                'id_card_masked' => '810000201801****1234',
                'age_group' => 0,
                'phone' => '13800000001',
                'password_hint' => 'test1234',
                'status' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'game_id' => $gameId,
                'name' => '测试少年A',
                'id_card' => '810000201201011234',
                'id_card_masked' => '810000201201****1234',
                'age_group' => 1,
                'phone' => '13800000002',
                'password_hint' => 'test1234',
                'status' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'game_id' => $gameId,
                'name' => '测试少年B',
                'id_card' => '810000200901011234',
                'id_card_masked' => '810000200901****1234',
                'age_group' => 2,
                'phone' => '13800000003',
                'password_hint' => 'test1234',
                'status' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'game_id' => $gameId,
                'name' => '测试成年',
                'id_card' => '810000200001011234',
                'id_card_masked' => '810000200001****1234',
                'age_group' => 3,
                'phone' => '13800000004',
                'password_hint' => 'test1234',
                'status' => 1,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ];

        foreach ($accounts as $account) {
            DB::table('kr_test_accounts')->updateOrInsert(
                ['game_id' => $gameId, 'age_group' => $account['age_group']],
                $account
            );
        }

        return array_map(fn ($a) => [
            'name' => $a['name'],
            'id_card' => $a['id_card_masked'],
            'age_group' => $a['age_group'],
            'age_group_label' => $this->getAgeGroupLabel($a['age_group']),
            'phone' => $a['phone'],
            'password' => $a['password_hint'],
        ], $accounts);
    }

    private function getAgeGroupLabel(int $ageGroup): string
    {
        return match ($ageGroup) {
            0 => '未满8周岁',
            1 => '8周岁以上未满16周岁',
            2 => '16周岁以上未满18周岁',
            3 => '已满18周岁',
            default => '未知',
        };
    }
}
