<?php
declare(strict_types=1);

namespace App\Modules\PlayerAffairs\Services;

use App\Core\EventBus;
use App\Modules\PlayerAffairs\Models\Player;

final class PlayerCardService
{
    /**
     * Activate a player's card.
     */
    public static function activateCard(int $playerId): void
    {
        $player = Player::find($playerId);
        if (!$player) {
            throw new \RuntimeException('اللاعب غير موجود');
        }

        $player->update(['card_status' => 'active']);

        EventBus::dispatch('player.card_activated', ['player_id' => $playerId]);
    }

    /**
     * Suspend a player's card.
     */
    public static function suspendCard(int $playerId, ?string $reason = null): void
    {
        $player = Player::find($playerId);
        if (!$player) {
            throw new \RuntimeException('اللاعب غير موجود');
        }

        $player->update(['card_status' => 'suspended']);

        EventBus::dispatch('player.card_suspended', [
            'player_id' => $playerId,
            'reason'    => $reason,
        ]);
    }

    /**
     * Revoke a player's card.
     */
    public static function revokeCard(int $playerId): void
    {
        $player = Player::find($playerId);
        if (!$player) {
            throw new \RuntimeException('اللاعب غير موجود');
        }

        $player->update(['card_status' => 'revoked']);

        EventBus::dispatch('player.card_revoked', ['player_id' => $playerId]);
    }

    /**
     * Check if a player's card is active.
     */
    public static function isCardActive(int $playerId): bool
    {
        $player = Player::find($playerId);
        if (!$player) {
            return false;
        }

        return $player->card_status === 'active';
    }
}
