<?php

namespace App\Http\Requests\Settings;

use App\Support\NotificationSettings;
use Illuminate\Foundation\Http\FormRequest;

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

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $rules = [
            'app_name' => ['required', 'string', 'max:255'],
            'timezone' => ['nullable', 'string', 'max:100'],
            'locale' => ['nullable', 'string', 'max:10'],
            'currency' => ['nullable', 'string', 'in:INR'],
            'company_name' => ['nullable', 'string', 'max:255'],
            'company_email' => ['nullable', 'email', 'max:255'],
            'company_phone' => ['nullable', 'string', 'max:50'],
            'company_address' => ['nullable', 'string', 'max:1000'],
            'mail_host' => ['nullable', 'string', 'max:255'],
            'mail_port' => ['nullable', 'integer', 'min:1', 'max:65535'],
            'mail_username' => ['nullable', 'string', 'max:255'],
            'mail_password' => ['nullable', 'string', 'max:255'],
            'mail_from_address' => ['nullable', 'email', 'max:255'],
            'mail_from_name' => ['nullable', 'string', 'max:255'],
            'whatsapp_enabled' => ['nullable', 'boolean'],
            'whatsapp_api_url' => ['nullable', 'string', 'max:2000', 'required_if:whatsapp_enabled,1'],
            'password_min_length' => ['nullable', 'integer', 'min:6', 'max:128'],
            'session_timeout' => ['nullable', 'integer', 'min:5', 'max:1440'],
            'require_strong_password' => ['nullable', 'boolean'],
            'two_factor_enabled' => ['nullable', 'boolean'],
            'allow_registration' => ['nullable', 'boolean'],
            'theme' => ['nullable', 'string', 'max:20'],
            'primary_color' => ['nullable', 'string', 'max:20'],
            'logo' => ['nullable', 'image', 'max:2048'],
            'homepage_template' => ['nullable', 'string', 'in:'.implode(',', array_keys(config('homepage.templates', [])))],
            'homepage_headline' => ['nullable', 'string', 'max:255'],
            'homepage_subtitle' => ['nullable', 'string', 'max:255'],
            'homepage_description' => ['nullable', 'string', 'max:10000'],
            'homepage_button_text' => ['nullable', 'string', 'max:100'],
            'homepage_feature_1' => ['nullable', 'string', 'max:255'],
            'homepage_feature_2' => ['nullable', 'string', 'max:255'],
            'homepage_feature_3' => ['nullable', 'string', 'max:255'],
            'homepage_hero_image' => ['nullable', 'image', 'mimes:jpeg,jpg,png,webp', 'max:4096'],
            'remove_homepage_hero_image' => ['nullable', 'boolean'],
            'settings_active_tab' => ['nullable', 'string', 'max:50'],
        ];

        foreach (NotificationSettings::eventKeys() as $eventKey) {
            $rules["notifications.{$eventKey}.email_enabled"] = ['nullable', 'boolean'];
            $rules["notifications.{$eventKey}.email_subject"] = ['nullable', 'string', 'max:255'];
            $rules["notifications.{$eventKey}.email_body"] = ['nullable', 'string', 'max:10000'];
            $rules["notifications.{$eventKey}.sms_enabled"] = ['nullable', 'boolean'];
            $rules["notifications.{$eventKey}.sms_body"] = ['nullable', 'string', 'max:2000'];
        }

        return $rules;
    }
}
