<?php

namespace App\Http\Requests\PlayerGameProfiles;

use App\Models\Game;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StorePlayerGameProfileRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('create', \App\Models\PlayerGameProfile::class) ?? false;
    }

    public function rules(): array
    {
        $game = $this->resolveGame();

        return array_merge($this->baseRules($game), [
            'game_id' => ['required', 'integer', Rule::exists('games', 'id')],
        ]);
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'is_primary_game' => $this->boolean('is_primary_game'),
            'is_public' => $this->boolean('is_public', true),
            'role_ids' => $this->input('role_ids', []),
            'character_ids' => $this->input('character_ids', []),
            'platform_ids' => $this->input('platform_ids', []),
        ]);
    }

    private function resolveGame(): ?Game
    {
        $gameId = $this->input('game_id');

        return filled($gameId) ? Game::query()->find($gameId) : null;
    }

    private function baseRules(?Game $game): array
    {
        return [
            'in_game_name' => ['required', 'string', 'max:255'],
            'external_game_id' => ['nullable', 'string', 'max:255'],
            'current_rank_id' => ['nullable', 'integer', Rule::exists('game_ranks', 'id')->where(fn ($query) => $query->where('game_id', $game?->id))],
            'highest_rank_id' => ['nullable', 'integer', Rule::exists('game_ranks', 'id')->where(fn ($query) => $query->where('game_id', $game?->id))],
            'primary_platform_id' => ['nullable', 'integer', Rule::exists('game_platforms', 'id')->where(fn ($query) => $query->where('game_id', $game?->id))],
            'region_id' => ['nullable', 'integer', Rule::exists('game_regions', 'id')->where(fn ($query) => $query->where('game_id', $game?->id))],
            'server_id' => ['nullable', 'integer', Rule::exists('game_servers', 'id')->where(fn ($query) => $query->where('game_id', $game?->id))],
            'years_of_experience' => ['nullable', 'integer', 'min:0', 'max:50'],
            'skill_level' => ['nullable', 'string', 'max:50'],
            'biography' => ['nullable', 'string', 'max:2000'],
            'is_primary_game' => ['sometimes', 'boolean'],
            'is_public' => ['sometimes', 'boolean'],
            'role_ids' => ['nullable', 'array'],
            'role_ids.*' => ['integer', Rule::exists('game_roles', 'id')->where(fn ($query) => $query->where('game_id', $game?->id))],
            'primary_role_id' => ['nullable', 'integer', Rule::exists('game_roles', 'id')->where(fn ($query) => $query->where('game_id', $game?->id))],
            'character_ids' => ['nullable', 'array'],
            'character_ids.*' => ['integer', Rule::exists('game_characters', 'id')->where(fn ($query) => $query->where('game_id', $game?->id))],
            'platform_ids' => ['nullable', 'array'],
            'platform_ids.*' => ['integer', Rule::exists('game_platforms', 'id')->where(fn ($query) => $query->where('game_id', $game?->id))],
        ];
    }
}
