<?php

namespace App\Http\Requests\Records;

use App\Enums\ProfileVisibility;
use App\Enums\RecordType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    public function rules(): array
    {
        $gameId = $this->integer('game_id');

        return [
            'title' => ['required', 'string', 'max:255'],
            'description' => ['nullable', 'string', 'max:5000'],
            'game_id' => ['required', 'integer', 'exists:games,id'],
            'player_game_profile_id' => ['required', 'integer', 'exists:player_game_profiles,id'],
            'record_type' => ['required', Rule::enum(RecordType::class)],
            'achievement_date' => ['nullable', 'date'],
            'result_text' => ['nullable', 'string', 'max:255'],
            'rank_id' => [
                'nullable',
                'integer',
                Rule::exists('game_ranks', 'id')->where(fn ($query) => $query->where('game_id', $gameId)),
            ],
            'visibility' => ['nullable', Rule::enum(ProfileVisibility::class)],
            'screenshots' => ['nullable', 'array', 'max:5'],
            'screenshots.*' => ['image', 'mimes:jpg,jpeg,png,webp', 'max:4096'],
        ];
    }
}
