<?php

namespace App\Http\Requests;

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

class StoreSmsSenderRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('manage-settings') ?? false;
    }

    public function rules(): array
    {
        $pid = (int) $this->input('profile_id');

        return [
            'profile_id' => ['required', 'integer', 'exists:sms_profiles,id'],
            'sname' => [
                'nullable', 'string', 'max:20',
                Rule::unique('sms_senders', 'sname')->where(fn ($q) => $q->where('profile_id', $pid)),
            ],
            'phone_number' => ['nullable', 'string', 'max:30'],
            'description' => ['nullable', 'string', 'max:190'],
            'active' => ['sometimes', 'boolean'],
        ];
    }
}
