<?php

namespace App\Services\Admin;

use App\Models\AdminUser;
use App\Models\AgentNode;
use App\Models\AgentProfile;
use App\Models\Player;
use App\Support\AdminScopeContext;
use App\Support\AdminScopeContextResolver;
use App\Services\AgentSettlement\ShareLedgerScopedProfitAggregator;
use App\Support\AdminAgentSettlementScope;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/** 代理账号仪表盘：授信、团队规模、待结账单摘要。 */
final class AgentDashboardOverviewBuilder
{
    public function __construct(
        private readonly AdminReportQueryService $reportQuery,
        private readonly ShareLedgerScopedProfitAggregator $shareProfitAggregator,
    ) {}

    /**
     * @return array<string, mixed>|null
     */
    public function build(AdminUser $admin): ?array
    {
        if (! $admin->hasPermissionCode('dashboard.view')) {
            return null;
        }

        $node = $admin->primaryAgentNode();
        if ($node === null) {
            return null;
        }

        $profile = AgentProfile::query()->where('agent_node_id', $node->id)->first();
        $subtreeIds = AgentNode::query()
            ->where('path', 'like', $node->path.'%')
            ->pluck('id')
            ->map(static fn ($id): int => (int) $id)
            ->all();

        $directChildCount = AgentNode::query()
            ->where('parent_id', $node->id)
            ->count();

        $directPlayerCount = 0;
        if (Schema::hasColumn('players', 'agent_node_id')) {
            $directPlayerCount = Player::query()
                ->where('agent_node_id', $node->id)
                ->count();
        }

        $pendingBillStats = $this->pendingBillStats($admin, $subtreeIds);
        $scope = AdminScopeContextResolver::fromValues($admin, requestedAgentNodeId: (int) $node->id);
        $today = now()->toDateString();
        $sevenDayFrom = now()->subDays(6)->toDateString();
        $todayTotals = $this->reportQuery->periodFinanceTotals($today, $today, $scope);
        $sevenDayTotals = $this->reportQuery->periodFinanceTotals($sevenDayFrom, $today, $scope);
        $todayShareProfit = $this->shareProfitAggregator->sumShareProfitForAdmin($admin, $today, $today);
        $sevenDayShareProfit = $this->shareProfitAggregator->sumShareProfitForAdmin($admin, $sevenDayFrom, $today);
        $currencyCode = $this->reportQuery->resolvePeriodCurrencyCode($today, $today, $scope)
            ?? $this->reportQuery->resolvePeriodCurrencyCode($sevenDayFrom, $today, $scope);
        $teamPlayerStats = $this->teamPlayerStats($subtreeIds);
        $todayActivityStats = $this->todayActivityStats($subtreeIds, $today);
        $topAgentToday = $this->topAgentToday($scope, $today, (int) $node->id);

        return [
            'agent_node_id' => (int) $node->id,
            'agent_code' => (string) $node->code,
            'agent_name' => (string) $node->name,
            'depth' => (int) $node->depth,
            'credit_limit' => (int) ($profile?->credit_limit ?? 0),
            'allocated_credit' => (int) ($profile?->allocated_credit ?? 0),
            'used_credit' => (int) ($profile?->used_credit ?? 0),
            'available_credit' => max(
                0,
                (int) ($profile?->credit_limit ?? 0) - (int) ($profile?->allocated_credit ?? 0),
            ),
            'total_share_rate' => (float) ($profile?->total_share_rate ?? 0),
            'can_create_child_agent' => (bool) ($profile?->can_create_child_agent ?? false),
            'can_create_player' => (bool) ($profile?->can_create_player ?? false),
            'direct_child_count' => $directChildCount,
            'subtree_agent_count' => count($subtreeIds),
            'direct_player_count' => $directPlayerCount,
            'team_player_count' => $teamPlayerStats['count'],
            'active_player_count_today' => $todayActivityStats['player_count'],
            'bet_order_count_today' => $todayActivityStats['order_count'],
            'today_bet_minor' => $todayTotals['total_bet_minor'],
            'today_payout_minor' => $todayTotals['total_payout_minor'],
            'today_profit_minor' => $todayShareProfit,
            'seven_day_bet_minor' => $sevenDayTotals['total_bet_minor'],
            'seven_day_payout_minor' => $sevenDayTotals['total_payout_minor'],
            'seven_day_profit_minor' => $sevenDayShareProfit,
            'profit_scope' => 'share_profit',
            'currency_code' => $currencyCode,
            'pending_bill_count' => $pendingBillStats['count'],
            'pending_unpaid_minor' => $pendingBillStats['unpaid_minor'],
            'latest_bet_at' => $todayActivityStats['latest_bet_at'],
            'top_agent_today' => $topAgentToday,
        ];
    }

    /**
     * @param  list<int>  $subtreeIds
     * @return array{count: int, unpaid_minor: int}
     */
    private function pendingBillStats(AdminUser $admin, array $subtreeIds): array
    {
        if ($subtreeIds === []) {
            return ['count' => 0, 'unpaid_minor' => 0];
        }

        $query = DB::table('settlement_bills')
            ->where('bill_type', 'agent')
            ->where('owner_type', 'agent')
            ->whereIn('owner_id', $subtreeIds)
            ->whereIn('status', ['pending', 'pending_confirm', 'partial']);

        AdminAgentSettlementScope::applyToBillsQuery($query, $admin);

        return [
            'count' => (int) $query->count(),
            'unpaid_minor' => (int) $query->sum('unpaid_amount'),
        ];
    }

    /**
     * @param  list<int>  $subtreeIds
     * @return array{count: int}
     */
    private function teamPlayerStats(array $subtreeIds): array
    {
        if ($subtreeIds === [] || ! Schema::hasColumn('players', 'agent_node_id')) {
            return ['count' => 0];
        }

        return [
            'count' => (int) Player::query()
                ->whereIn('agent_node_id', $subtreeIds)
                ->count(),
        ];
    }

    /**
     * @param  list<int>  $subtreeIds
     * @return array{player_count: int, order_count: int, latest_bet_at: ?string}
     */
    private function todayActivityStats(array $subtreeIds, string $today): array
    {
        if ($subtreeIds === []) {
            return [
                'player_count' => 0,
                'order_count' => 0,
                'latest_bet_at' => null,
            ];
        }

        $base = DB::table('ticket_orders as o')
            ->join('draws as d', 'd.id', '=', 'o.draw_id')
            ->join('players as p', 'p.id', '=', 'o.player_id')
            ->whereIn('p.agent_node_id', $subtreeIds)
            ->whereDate('d.business_date', $today);

        $latestBetAt = (clone $base)->max('o.created_at');

        return [
            'player_count' => (int) (clone $base)->distinct('o.player_id')->count('o.player_id'),
            'order_count' => (int) (clone $base)->count(),
            'latest_bet_at' => $latestBetAt !== null ? Carbon::parse((string) $latestBetAt)->toIso8601String() : null,
        ];
    }

    /**
     * @return array<string, mixed>|null
     */
    private function topAgentToday(AdminScopeContext $scope, string $today, int $currentAgentId): ?array
    {
        $rows = $this->reportQuery->agentRankingRows($today, $today, null, 5, $scope);
        foreach ($rows as $row) {
            if ((int) ($row['agent_node_id'] ?? 0) === $currentAgentId) {
                return $row;
            }
        }

        return $rows[0] ?? null;
    }
}
