<?php

namespace App\Http\Requests\Settings;

use Illuminate\Foundation\Http\FormRequest;

/**
 * Validasi input setting admin per tab (smtp | duitku | rate).
 */
class AdminSettingUpdateRequest extends FormRequest
{
    public function authorize(): bool
    {
        return (bool) $this->user()?->isAdmin();
    }

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

        return match ($tab) {
            'smtp' => [
                'tab' => ['required', 'in:smtp'],
                '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_encryption' => ['nullable', 'in:tls,ssl,none'],
                'mail_from_address' => ['nullable', 'email', 'max:255'],
                'mail_from_name' => ['nullable', 'string', 'max:255'],
                'mail_default' => ['nullable', 'in:smtp,log,sendmail'],
            ],
            'duitku' => [
                'tab' => ['required', 'in:duitku'],
                'duitku_enabled' => ['nullable', 'boolean'],
                'duitku_merchant_code' => ['nullable', 'string', 'max:255'],
                'duitku_api_key' => ['nullable', 'string', 'max:255'],
                'duitku_base_url' => ['nullable', 'string', 'max:255'],
                'duitku_inquiry_path' => ['nullable', 'string', 'max:255'],
                'duitku_callback_url' => ['nullable', 'string', 'max:255'],
                'duitku_return_url' => ['nullable', 'string', 'max:255'],
                'duitku_expiry_period' => ['nullable', 'integer', 'min:1', 'max:10080'],
            ],
            'rate' => [
                'tab' => ['required', 'in:rate'],
                'default_rate' => ['required', 'integer', 'min:1'],
                'empty_kategori_url' => ['nullable', 'string', 'max:1000', 'url'],
            ],
            'website' => [
                'tab' => ['required', 'in:website'],
                'logo' => ['nullable', 'image', 'mimes:png,jpg,jpeg,svg,webp', 'max:2048'],
                'favicon' => ['nullable', 'file', 'mimes:png,ico,svg', 'max:512'],
                'remove_logo' => ['nullable', 'boolean'],
                'remove_favicon' => ['nullable', 'boolean'],
            ],
            default => [
                'tab' => ['required', 'in:smtp,duitku,rate,website'],
            ],
        };
    }
}
