<?php

namespace App\Livewire\Admin;

use App\Services\Secrets\SecretVault;
use LivewireUI\Modal\ModalComponent;

/**
 * Confirmation before a credential takes effect (R23).
 *
 * The value being saved lives only in Secrets' own (deferred) `entered`
 * property — this modal never sees it and cannot mutate anything itself.
 * Confirming dispatches back to the page component, whose save() keeps its
 * own guard() (capability + recent password) unchanged.
 */
class ConfirmSaveSecret extends ModalComponent
{
    public string $key;

    public string $label = '';

    public function mount(string $key): void
    {
        $this->authorize('secrets.manage');
        abort_if(! array_key_exists($key, SecretVault::REGISTRY), 404);

        $this->key = $key;
        $this->label = __(SecretVault::REGISTRY[$key]['label']);
    }

    public function confirm(): void
    {
        $this->authorize('secrets.manage');
        $this->dispatch('secret-save-confirmed', key: $this->key);
        $this->closeModal();
    }

    public function render()
    {
        return view('livewire.admin.confirm-save-secret');
    }
}
