<?php

namespace App\Http\Requests;

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

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

    public function rules(): array
    {
        $id = (int) ($this->route('smsTemplate')?->id ?? 0);

        return [
            'name' => ['required', 'string', 'max:190'],
            'slug' => ['required', 'string', 'max:190', 'alpha_dash', Rule::unique('sms_templates', 'slug')->ignore($id)],
            // Content is required unless template allows custom body
            'content' => [Rule::requiredIf(fn () => ! (bool) $this->input('allow_custom_body')), 'nullable', 'string', 'max:1000'],
            'variables_json' => ['nullable', 'array'],
            'is_active' => ['sometimes', 'boolean'],
            'default_profile_id' => ['nullable', 'integer', 'exists:sms_profiles,id'],
            'default_sender_id' => ['nullable', 'integer', 'exists:sms_senders,id'],
            'allow_custom_body' => ['sometimes', 'boolean'],
            'action_id' => ['nullable', 'integer', 'exists:actions,id'],
            'decision_id' => ['nullable', 'integer', 'exists:decisions,id'],
        ];
    }
}
