<?php

namespace App\Livewire\Admin;

use App\Models\Instance;
use LivewireUI\Modal\ModalComponent;

/**
 * Confirmation before an operator restarts a customer's machine (R23).
 *
 * The customer is not asked and may be working in it, so the address is spelled
 * out in the question — an operator confirming a restart should be reading the
 * hostname of the cloud that is about to go away, not the position of a row in a
 * paginated table.
 *
 * No mutation here. Instances::restart() keeps the work and both authorisation
 * checks it already had.
 */
class ConfirmRestartInstance extends ModalComponent
{
    public string $uuid;

    public string $address = '';

    public function mount(string $uuid): void
    {
        // A modal is reachable without the page's own route middleware (R20), so
        // it resolves the record itself rather than trusting a property the
        // browser hydrated.
        $instance = Instance::query()->where('uuid', $uuid)->firstOrFail();

        $this->uuid = $uuid;
        $this->address = $instance->domainIsVerified()
            ? (string) $instance->custom_domain
            : (string) $instance->subdomain;
    }

    public function proceed(): void
    {
        $this->dispatch('instance-restart-confirmed', uuid: $this->uuid);
        $this->closeModal();
    }

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