<?php

namespace App\Http\Requests;

use App\Rules\MaxWordCount;
use Illuminate\Foundation\Http\FormRequest;

class UpdateTopbarNotificationRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('edit topbar notifications') ?? false;
    }

    public function rules(): array
    {
        return [
            'content'      => ['required', 'string', 'max:255', new MaxWordCount(7)],
            'button_label' => ['required', 'string', 'max:50'],
            'button_url'   => ['required', 'url', 'max:500'],
            'start_date'   => ['nullable', 'date'],
            'end_date'     => ['nullable', 'date', 'after_or_equal:start_date'],
            'is_active'    => ['nullable', 'boolean'],
            'order'        => ['nullable', 'integer', 'min:0'],
        ];
    }

    public function messages(): array
    {
        return [
            'end_date.after_or_equal' => 'The end date must be on or after the start date.',
        ];
    }
}
