<form action="{{ $action }}" method="POST" class="card-arena p-4">
    @csrf
    @if($method !== 'POST')
        @method($method)
    @endif

    @if($profile === null)
        <div class="mb-3">
            <label class="form-label text-white">Game</label>
            <select name="game_id" class="form-select" required>
                <option value="">Select a game</option>
                @foreach($games as $game)
                    <option value="{{ $game->id }}" @selected(old('game_id') == $game->id)>{{ $game->name }}</option>
                @endforeach
            </select>
            @error('game_id')<div class="text-danger fs-sm">{{ $message }}</div>@enderror
        </div>
    @endif

    <div class="mb-3">
        <label class="form-label text-white">In-Game Name</label>
        <input type="text" name="in_game_name" value="{{ old('in_game_name', $profile?->in_game_name) }}" class="form-control" required>
        @error('in_game_name')<div class="text-danger fs-sm">{{ $message }}</div>@enderror
    </div>

    <div class="mb-3">
        <label class="form-label text-white">External Game ID</label>
        <input type="text" name="external_game_id" value="{{ old('external_game_id', $profile?->external_game_id) }}" class="form-control">
    </div>

    @php($selectedGame = $profile?->game ?? $games->firstWhere('id', old('game_id')))

    @if($selectedGame)
        <div class="row g-3">
            @if($selectedGame->supports_ranks)
                <div class="col-md-6">
                    <label class="form-label text-white">Current Rank</label>
                    <select name="current_rank_id" class="form-select">
                        <option value="">None</option>
                        @foreach($selectedGame->ranks as $rank)
                            <option value="{{ $rank->id }}" @selected(old('current_rank_id', $profile?->current_rank_id) == $rank->id)>{{ $rank->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-6">
                    <label class="form-label text-white">Highest Rank</label>
                    <select name="highest_rank_id" class="form-select">
                        <option value="">None</option>
                        @foreach($selectedGame->ranks as $rank)
                            <option value="{{ $rank->id }}" @selected(old('highest_rank_id', $profile?->highest_rank_id) == $rank->id)>{{ $rank->name }}</option>
                        @endforeach
                    </select>
                </div>
            @endif

            @if($selectedGame->platforms->isNotEmpty())
                <div class="col-md-6">
                    <label class="form-label text-white">Primary Platform</label>
                    <select name="primary_platform_id" class="form-select">
                        <option value="">None</option>
                        @foreach($selectedGame->platforms as $platform)
                            <option value="{{ $platform->id }}" @selected(old('primary_platform_id', $profile?->primary_platform_id) == $platform->id)>{{ $platform->name }}</option>
                        @endforeach
                    </select>
                </div>
            @endif

            @if($selectedGame->regions->isNotEmpty())
                <div class="col-md-6">
                    <label class="form-label text-white">Region</label>
                    <select name="region_id" class="form-select">
                        <option value="">None</option>
                        @foreach($selectedGame->regions as $region)
                            <option value="{{ $region->id }}" @selected(old('region_id', $profile?->region_id) == $region->id)>{{ $region->name }}</option>
                        @endforeach
                    </select>
                </div>
            @endif

            @if($selectedGame->supports_roles && $selectedGame->roles->isNotEmpty())
                <div class="col-12">
                    <label class="form-label text-white">Roles</label>
                    <div class="d-flex flex-wrap gap-3">
                        @foreach($selectedGame->roles as $role)
                            <label class="form-check">
                                <input class="form-check-input" type="checkbox" name="role_ids[]" value="{{ $role->id }}"
                                    @checked(in_array($role->id, old('role_ids', $profile?->gameRoles->pluck('game_role_id')->all() ?? [])))>
                                <span class="form-check-label text-white">{{ $role->name }}</span>
                            </label>
                        @endforeach
                    </div>
                </div>
            @endif
        </div>
    @endif

    <div class="mb-3 mt-3">
        <label class="form-label text-white">Biography</label>
        <textarea name="biography" rows="4" class="form-control">{{ old('biography', $profile?->biography) }}</textarea>
    </div>

    <div class="d-flex flex-wrap gap-3 mb-3">
        <label class="form-check">
            <input class="form-check-input" type="checkbox" name="is_primary_game" value="1" @checked(old('is_primary_game', $profile?->is_primary_game))>
            <span class="form-check-label text-white">Primary game</span>
        </label>
        <label class="form-check">
            <input class="form-check-input" type="checkbox" name="is_public" value="1" @checked(old('is_public', $profile?->is_public ?? true))>
            <span class="form-check-label text-white">Public profile</span>
        </label>
    </div>

    <button type="submit" class="btn-arena btn-primary btn-sm">Save</button>
    <a href="{{ route('profile.games.index') }}" class="btn-arena btn-sm btn-outline-light">Cancel</a>
</form>
