<?php

namespace App\Http\Requests;

use App\Models\User;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProfileUpdateRequest extends FormRequest
{
    /**
     * Normalize public profile values before validation so route-reserved words
     * are checked consistently.
     */
    protected function prepareForValidation(): void
    {
        if ($this->has('public_username')) {
            $this->merge([
                'public_username' => filled($this->input('public_username'))
                    ? str($this->input('public_username'))->lower()->toString()
                    : null,
            ]);
        }
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            'email' => [
                'required',
                'string',
                'lowercase',
                'email',
                'max:255',
                Rule::unique(User::class)->ignore($this->user()->id),
            ],
            'public_username' => [
                'nullable',
                'string',
                'alpha_dash',
                'min:3',
                'max:40',
                Rule::notIn([
                    'install',
                    'admin',
                    'support-team',
                    'mentor',
                    'student',
                    'auth',
                    'login',
                    'register',
                    'dashboard',
                    'trades',
                    'trading-tools',
                    'trading-accounts',
                    'forex-calendar',
                    'market-news',
                    'pricing',
                    'support',
                    'payments',
                    'subscriptions',
                    'profile',
                    'referrals',
                    'maintenance',
                    'challenge',
                    'reports',
                    'waitlist',
                    'up',
                ]),
                Rule::unique(User::class)->ignore($this->user()->id),
            ],
            'public_profile_enabled' => ['nullable', 'boolean'],
            'show_public_profit' => ['nullable', 'boolean'],
        ];
    }
}
