<?php

namespace App\Concerns;

use App\Models\User;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Validation\Rule;

trait ProfileValidationRules
{
    /**
     * Get the validation rules used to validate user profiles.
     *
     * @return array<string, array<int, ValidationRule|array<mixed>|string>>
     */
    protected function profileRules(?int $userId = null): array
    {
        return [
            'first_name' => $this->nameRules(),
            'last_name' => $this->nameRules(),
            'email' => $this->emailRules($userId),
            'contact_number' => ['required', 'string', 'max:50'],
            'country' => ['required', 'string', Rule::in($this->countryCodes())],
        ];
    }

    /**
     * Get the validation rules used to validate user names.
     *
     * @return array<int, ValidationRule|array<mixed>|string>
     */
    protected function nameRules(): array
    {
        return ['required', 'string', 'max:255'];
    }

    /**
     * Get the validation rules used to validate user emails.
     *
     * @return array<int, ValidationRule|array<mixed>|string>
     */
    protected function emailRules(?int $userId = null): array
    {
        return [
            'required',
            'string',
            'email',
            'max:255',
            $userId === null
                ? Rule::unique(User::class)
                : Rule::unique(User::class)->ignore($userId),
        ];
    }

    /**
     * Get the supported country codes.
     *
     * @return array<int, string>
     */
    protected function countryCodes(): array
    {
        return [
            'AR',
            'BO',
            'BR',
            'CL',
            'CO',
            'CR',
            'CU',
            'DO',
            'EC',
            'SV',
            'GT',
            'HN',
            'MX',
            'NI',
            'PA',
            'PY',
            'PE',
            'ES',
            'UY',
            'VE',
            'US',
            'OTHER',
        ];
    }
}
