<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;

class StoreProfileRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->user() !== null
            && $this->user()->hasAnalyzedResume();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'username' => [
                'nullable',
                'string',
                'min:3',
                'max:80',
                'alpha_dash:ascii',
                Rule::unique('users', 'username')->ignore($this->user()?->id),
            ],
            'current_password' => [
                Rule::requiredIf(fn (): bool => filled($this->input('new_password')) && filled($this->user()?->password_set_at)),
                'nullable',
                'current_password:web',
            ],
            'new_password' => ['nullable', 'confirmed', Password::min(8)->mixedCase()->numbers()],
            'headline' => ['required', 'string', 'max:160'],
            'summary' => ['nullable', 'string', 'max:2000'],
            'current_role' => ['nullable', 'string', 'max:120'],
            'experience_level' => ['required', 'string', Rule::in(['entry', 'junior', 'mid', 'senior', 'lead', 'manager', 'executive'])],
            'country_code' => ['nullable', 'string', 'size:2'],
            'city' => ['nullable', 'string', 'max:120'],
            'timezone' => ['nullable', 'string', 'max:80'],
            'skills' => ['required', 'array', 'min:1', 'max:40'],
            'skills.*' => ['string', 'max:80'],
            'preferred_roles' => ['required', 'array', 'min:1', 'max:15'],
            'preferred_roles.*' => ['string', 'max:120'],
            'preferred_categories' => ['required', 'array', 'min:1', 'max:8'],
            'preferred_categories.*' => ['string', 'max:80'],
            'links' => ['nullable', 'array', 'max:5'],
            'links.*' => ['nullable', 'url', 'max:2048'],
            'availability' => ['required', 'string', Rule::in(['immediate', 'two_weeks', 'one_month', 'flexible'])],
            'salary_currency' => ['required', 'string', 'size:3'],
            'salary_min' => ['nullable', 'integer', 'min:0', 'max:10000000'],
            'open_to_contract' => ['required', 'boolean'],
            'open_to_full_time' => ['required', 'boolean'],
        ];
    }
}
