<?php

namespace App\Http\Requests\Admin;

use App\Models\AdminUser;
use App\Http\Requests\ApiFormRequest;
use App\Support\AdminSettingPolicy;

final class AdminSettingUpdateRequest extends ApiFormRequest
{
    public function authorize(): bool
    {
        $admin = $this->lotteryAdmin();
        if (! $admin instanceof AdminUser) {
            return false;
        }

        $key = (string) $this->route('key', '');
        return AdminSettingPolicy::canUpdate($admin, $key);
    }

    public function rules(): array
    {
        return [
            'value' => ['present'],
        ];
    }

    public function after(): array
    {
        return [
            function (): void {
                $key = (string) $this->route('key', '');
                AdminSettingPolicy::validateItems([[
                    'key' => $key,
                    'value' => $this->validated('value'),
                ]]);
            },
        ];
    }
}
