<?php

namespace App\Http\Requests\Tenant;

use App\Services\Packages\TenantNotificationPreferences;
use Illuminate\Foundation\Http\FormRequest;

class NotificationPreferencesRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $rules = [
            'preferences' => ['required', 'array'],
        ];

        foreach (array_keys(TenantNotificationPreferences::defaults()) as $key) {
            $rules["preferences.{$key}"] = ['required', 'boolean'];
        }

        return $rules;
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'preferences' => $this->input('preferences', []),
        ]);
    }
}
