<?php

/**
 * 文件功能：双色球彩票 HTTP 控制器
 *
 * 提供前端所需的四个 API 接口：
 * - current() : 当期状态 + 奖池 + 我的购票列表
 * - buy()     : 购买一注或多注（支持机选）
 * - history() : 历史期次列表
 * - my()      : 我的全部购票记录
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

namespace App\Http\Controllers;

use App\Models\GameConfig;
use App\Models\LotteryIssue;
use App\Models\LotteryTicket;
use App\Services\LotteryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LotteryController extends Controller
{
    public function __construct(
        private readonly LotteryService $lottery,
    ) {}

    /**
     * 返回当期状态：期号、奖池、剩余时间、我本期购票列表。
     */
    public function current(): JsonResponse
    {
        if (! GameConfig::isEnabled('lottery')) {
            return response()->json(['enabled' => false]);
        }

        $issue = LotteryIssue::currentIssue() ?? LotteryIssue::latestIssue();

        if (! $issue) {
            return response()->json(['enabled' => true, 'issue' => null]);
        }

        $myTickets = LotteryTicket::query()
            ->where('issue_id', $issue->id)
            ->where('user_id', Auth::id())
            ->orderBy('id')
            ->get()
            ->map(fn ($t) => [
                'id' => $t->id,
                'numbers' => $t->numbersLabel(),
                'red1' => $t->red1,
                'red2' => $t->red2,
                'red3' => $t->red3,
                'blue' => $t->blue,
                'is_quick' => $t->is_quick_pick,
                'prize_level' => $t->prize_level,
                'payout' => $t->payout,
            ]);

        return response()->json([
            'enabled' => true,
            'is_open' => $issue->isOpen(),
            'issue' => [
                'id' => $issue->id,
                'issue_no' => $issue->issue_no,
                'status' => $issue->status,
                'pool_amount' => $issue->pool_amount,
                'is_super_issue' => $issue->is_super_issue,
                'no_winner_streak' => $issue->no_winner_streak,
                'seconds_left' => $issue->secondsUntilDraw(),
                'draw_at' => $issue->draw_at?->toDateTimeString(),
                'sell_closes_at' => $issue->sell_closes_at?->toDateTimeString(),
                'red1' => $issue->red1,
                'red2' => $issue->red2,
                'red3' => $issue->red3,
                'blue' => $issue->blue,
            ],
            'my_tickets' => $myTickets,
            'my_ticket_count' => $myTickets->count(),
        ]);
    }

    /**
     * 购票接口：支持自选和机选，支持一次购买多注。
     */
    public function buy(Request $request): JsonResponse
    {
        $request->validate([
            'numbers' => 'required|array|min:1',
            'numbers.*.reds' => 'required|array|size:3',
            'numbers.*.reds.*' => 'required|integer|min:1|max:12',
            'numbers.*.blue' => 'required|integer|min:1|max:6',
            'quick_pick' => 'boolean',
        ]);

        try {
            $tickets = $this->lottery->buyTickets(
                user: Auth::user(),
                numbers: $request->input('numbers'),
                quickPick: (bool) $request->input('quick_pick', false),
            );

            return response()->json([
                'status' => 'success',
                'message' => '购票成功！共 '.count($tickets).' 注',
                'count' => count($tickets),
            ]);
        } catch (\RuntimeException $e) {
            return response()->json(['status' => 'error', 'message' => $e->getMessage()], 422);
        }
    }

    /**
     * 机选号码接口（仅生成号码，不扣费，供前端展示后确认购买）。
     */
    public function quickPick(Request $request): JsonResponse
    {
        $count = min((int) $request->input('count', 1), 10);

        return response()->json([
            'numbers' => $this->lottery->quickPick($count),
        ]);
    }

    /**
     * 历史期次列表。
     */
    public function history(): JsonResponse
    {
        $issues = LotteryIssue::query()
            ->where('status', 'settled')
            ->latest()
            ->limit(20)
            ->get()
            ->map(fn ($i) => [
                'issue_no' => $i->issue_no,
                'red1' => $i->red1,
                'red2' => $i->red2,
                'red3' => $i->red3,
                'blue' => $i->blue,
                'pool_amount' => $i->pool_amount,
                'payout_amount' => $i->payout_amount,
                'total_tickets' => $i->total_tickets,
                'is_super_issue' => $i->is_super_issue,
                'no_winner_streak' => $i->no_winner_streak,
                'draw_at' => $i->draw_at?->toDateTimeString(),
            ]);

        return response()->json(['issues' => $issues]);
    }

    /**
     * 我的购票记录（跨期次）。
     */
    public function my(): JsonResponse
    {
        $tickets = LotteryTicket::query()
            ->where('user_id', Auth::id())
            ->with('issue:id,issue_no,status,red1,red2,red3,blue,draw_at')
            ->latest()
            ->limit(50)
            ->get()
            ->map(fn ($t) => [
                'issue_no' => $t->issue?->issue_no,
                'status' => $t->issue?->status,
                'numbers' => $t->numbersLabel(),
                'prize_level' => $t->prize_level,
                'payout' => $t->payout,
                'is_quick' => $t->is_quick_pick,
                'created_at' => $t->created_at->toDateTimeString(),
            ]);

        return response()->json(['tickets' => $tickets]);
    }
}
