<?php

namespace App\Support;

use App\Models\OddsItem;
use App\Models\PlayType;
use App\Models\OddsVersion;
use App\Models\RiskCapItem;
use App\Models\PlayConfigItem;
use App\Models\RiskCapVersion;
use App\Models\PlayConfigVersion;

/** 后台 API：阶段 4 运营配置序列化（与其它 Admin*Controller 手写数组风格一致）。 */
final class AdminConfigPresenter
{
    /** @return array<string, mixed> */
    public static function playType(PlayType $t): array
    {
        return [
            'id' => (int) $t->id,
            'play_code' => $t->play_code,
            'category' => $t->category,
            'dimension' => $t->dimension,
            'bet_mode' => $t->bet_mode,
            'display_name' => $t->display_name,
            'is_enabled' => (bool) $t->is_enabled,
            'sort_order' => (int) $t->sort_order,
            'supports_multi_number' => (bool) $t->supports_multi_number,
            'reserved_rule_json' => $t->reserved_rule_json,
            'updated_at' => $t->updated_at?->toIso8601String(),
        ];
    }

    /** @return array<string, mixed> */
    public static function playConfigVersionSummary(PlayConfigVersion $v): array
    {
        return [
            'id' => (int) $v->id,
            'version_no' => (int) $v->version_no,
            'status' => $v->status,
            'effective_at' => $v->effective_at?->toIso8601String(),
            'updated_by' => $v->updated_by,
            'reason' => $v->reason,
            'created_at' => $v->created_at?->toIso8601String(),
            'updated_at' => $v->updated_at?->toIso8601String(),
        ];
    }

    /** @return array<string, mixed> */
    public static function playConfigVersionDetail(PlayConfigVersion $v): array
    {
        $base = self::playConfigVersionSummary($v);
        $base['items'] = $v->items->map(fn (PlayConfigItem $r) => self::playConfigItem($r))->values()->all();

        return $base;
    }

    /** @return array<string, mixed> */
    public static function playConfigItem(PlayConfigItem $r): array
    {
        return [
            'id' => (int) $r->id,
            'play_code' => $r->play_code,
            'category' => $r->category,
            'dimension' => $r->dimension === null ? null : (int) $r->dimension,
            'bet_mode' => $r->bet_mode,
            'display_name' => $r->display_name,
            'is_enabled' => (bool) $r->is_enabled,
            'min_bet_amount' => (int) $r->min_bet_amount,
            'max_bet_amount' => (int) $r->max_bet_amount,
            'display_order' => (int) $r->display_order,
            'supports_multi_number' => (bool) $r->supports_multi_number,
            'reserved_rule_json' => $r->reserved_rule_json,
            'rule_text_zh' => $r->rule_text_zh,
            'rule_text_en' => $r->rule_text_en,
            'rule_text_ne' => $r->rule_text_ne,
            'extra_config_json' => $r->extra_config_json,
        ];
    }

    /** @return array<string, mixed> */
    public static function oddsVersionSummary(OddsVersion $v): array
    {
        return [
            'id' => (int) $v->id,
            'version_no' => (int) $v->version_no,
            'status' => $v->status,
            'effective_at' => $v->effective_at?->toIso8601String(),
            'updated_by' => $v->updated_by,
            'reason' => $v->reason,
            'created_at' => $v->created_at?->toIso8601String(),
            'updated_at' => $v->updated_at?->toIso8601String(),
        ];
    }

    /** @return array<string, mixed> */
    public static function oddsVersionDetail(OddsVersion $v): array
    {
        $base = self::oddsVersionSummary($v);
        $base['items'] = $v->items->map(fn (OddsItem $r) => self::oddsItem($r))->values()->all();

        return $base;
    }

    /** @return array<string, mixed> */
    public static function oddsItem(OddsItem $r): array
    {
        return [
            'id' => (int) $r->id,
            'play_code' => $r->play_code,
            'prize_scope' => $r->prize_scope,
            'odds_value' => (int) $r->odds_value,
            'rebate_rate' => (string) $r->rebate_rate,
            'commission_rate' => (string) $r->commission_rate,
            'currency_code' => $r->currency_code,
            'extra_config_json' => $r->extra_config_json,
        ];
    }

    /** @return array<string, mixed> */
    public static function riskCapVersionSummary(RiskCapVersion $v): array
    {
        return [
            'id' => (int) $v->id,
            'version_no' => (int) $v->version_no,
            'status' => $v->status,
            'effective_at' => $v->effective_at?->toIso8601String(),
            'updated_by' => $v->updated_by,
            'reason' => $v->reason,
            'created_at' => $v->created_at?->toIso8601String(),
            'updated_at' => $v->updated_at?->toIso8601String(),
        ];
    }

    /** @return array<string, mixed> */
    public static function riskCapVersionDetail(RiskCapVersion $v): array
    {
        $base = self::riskCapVersionSummary($v);
        $base['items'] = $v->items->map(fn (RiskCapItem $r) => self::riskCapItem($r))->values()->all();

        return $base;
    }

    /** @return array<string, mixed> */
    public static function riskCapItem(RiskCapItem $r): array
    {
        return [
            'id' => (int) $r->id,
            'draw_id' => $r->draw_id,
            'normalized_number' => $r->normalized_number,
            'cap_amount' => (int) $r->cap_amount,
            'cap_type' => $r->cap_type,
        ];
    }
}
