<?php

declare(strict_types=1);

namespace App\Http\Requests\Admin;

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

class UpdateSettingsRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    /** @return array<string, array<int, mixed>> */
    public function rules(): array
    {
        $group = $this->input('group', 'general');

        return match ($group) {
            'general' => $this->generalRules(),
            'api' => $this->apiRules(),
            'billing' => $this->billingRules(),
            'notifications' => $this->notificationRules(),
            'discord' => $this->discordRules(),
            default => ['group' => ['required', Rule::in(['general', 'api', 'billing', 'notifications', 'discord'])]],
        };
    }

    /** @return array<string, array<int, mixed>> */
    private function generalRules(): array
    {
        return [
            'group' => ['required', 'string'],
            'company_name' => ['nullable', 'string', 'max:255'],
            'company_email' => ['nullable', 'email', 'max:255'],
            'support_url' => ['nullable', 'url', 'max:500'],
            'status_page_url' => ['nullable', 'url', 'max:500'],
        ];
    }

    /** @return array<string, array<int, mixed>> */
    private function apiRules(): array
    {
        return [
            'group' => ['required', 'string'],
            'virtfusion_api_url' => ['nullable', 'url', 'max:500'],
            'virtfusion_api_token' => ['nullable', 'string', 'max:1000'],
            'pterodactyl_api_url' => ['nullable', 'url', 'max:500'],
            'pterodactyl_api_token' => ['nullable', 'string', 'max:1000'],
            'synergycp_api_url' => ['nullable', 'url', 'max:500'],
            'synergycp_api_token' => ['nullable', 'string', 'max:1000'],
            'enhance_api_url' => ['nullable', 'url', 'max:500'],
            'enhance_api_token' => ['nullable', 'string', 'max:1000'],
            'enhance_organization_id' => ['nullable', 'string', 'max:255'],
        ];
    }

    /** @return array<string, array<int, mixed>> */
    private function billingRules(): array
    {
        return [
            'group' => ['required', 'string'],
            'default_currency' => ['nullable', 'string', 'max:3'],
            'grace_period_days' => ['nullable', 'integer', 'min:0', 'max:365'],
            'suspension_warning_days' => ['nullable', 'integer', 'min:0', 'max:365'],
            'auto_terminate_days' => ['nullable', 'integer', 'min:0', 'max:365'],
            'bandwidth_overage_rate' => ['nullable', 'numeric', 'min:0', 'max:999.99'],
            'bandwidth_alert_75' => ['nullable', 'boolean'],
            'bandwidth_alert_90' => ['nullable', 'boolean'],
            'bandwidth_alert_100' => ['nullable', 'boolean'],
            'bandwidth_alert_75_email' => ['nullable', 'boolean'],
            'bandwidth_alert_90_email' => ['nullable', 'boolean'],
            'bandwidth_alert_100_email' => ['nullable', 'boolean'],
            'bandwidth_grace_period_days' => ['nullable', 'integer', 'min:0', 'max:365'],
            'bandwidth_auto_suspend' => ['nullable', 'boolean'],
        ];
    }

    /** @return array<string, array<int, mixed>> */
    private function notificationRules(): array
    {
        return [
            'group' => ['required', 'string'],
            'email_from_address' => ['nullable', 'email', 'max:255'],
            'email_from_name' => ['nullable', 'string', 'max:255'],
        ];
    }

    /** @return array<string, array<int, mixed>> */
    private function discordRules(): array
    {
        return [
            'group' => ['required', 'string'],
            'discord_payment_webhook_url' => ['nullable', 'url', 'max:500', 'regex:/^https:\/\/discord\.com\/api\/webhooks\//'],
            'discord_payment_webhook_enabled' => ['nullable', 'boolean'],
            'discord_provisioning_webhook_url' => ['nullable', 'url', 'max:500', 'regex:/^https:\/\/discord\.com\/api\/webhooks\//'],
            'discord_provisioning_webhook_enabled' => ['nullable', 'boolean'],
            'discord_support_webhook_url' => ['nullable', 'url', 'max:500', 'regex:/^https:\/\/discord\.com\/api\/webhooks\//'],
            'discord_support_webhook_enabled' => ['nullable', 'boolean'],
            'discord_system_webhook_url' => ['nullable', 'url', 'max:500', 'regex:/^https:\/\/discord\.com\/api\/webhooks\//'],
            'discord_system_webhook_enabled' => ['nullable', 'boolean'],
        ];
    }

    /** @return array<string, string> */
    public function messages(): array
    {
        return [
            'company_email.email' => 'Please enter a valid email address.',
            'support_url.url' => 'Please enter a valid URL.',
            'status_page_url.url' => 'Please enter a valid URL.',
            'virtfusion_api_url.url' => 'Please enter a valid URL.',
            'pterodactyl_api_url.url' => 'Please enter a valid URL.',
            'synergycp_api_url.url' => 'Please enter a valid URL.',
            'enhance_api_url.url' => 'Please enter a valid URL.',
            'email_from_address.email' => 'Please enter a valid email address.',
            'discord_payment_webhook_url.url' => 'Please enter a valid Discord webhook URL.',
            'discord_payment_webhook_url.regex' => 'Must be a valid Discord webhook URL (https://discord.com/api/webhooks/...).',
            'discord_provisioning_webhook_url.url' => 'Please enter a valid Discord webhook URL.',
            'discord_provisioning_webhook_url.regex' => 'Must be a valid Discord webhook URL (https://discord.com/api/webhooks/...).',
            'discord_support_webhook_url.url' => 'Please enter a valid Discord webhook URL.',
            'discord_support_webhook_url.regex' => 'Must be a valid Discord webhook URL (https://discord.com/api/webhooks/...).',
            'discord_system_webhook_url.url' => 'Please enter a valid Discord webhook URL.',
            'discord_system_webhook_url.regex' => 'Must be a valid Discord webhook URL (https://discord.com/api/webhooks/...).',
            'grace_period_days.integer' => 'Grace period must be a whole number.',
            'suspension_warning_days.integer' => 'Suspension warning days must be a whole number.',
            'auto_terminate_days.integer' => 'Auto-terminate days must be a whole number.',
            'bandwidth_overage_rate.numeric' => 'Bandwidth overage rate must be a number.',
            'bandwidth_grace_period_days.integer' => 'Bandwidth grace period must be a whole number.',
        ];
    }
}
