<?php

namespace App\Livewire\Modals;

use App\Models\HostCredential;
use App\Models\Server;
use App\Services\DockerService;
use Illuminate\Support\Facades\Auth;
use InvalidArgumentException;
use LivewireUI\Modal\ModalComponent;
use Throwable;

/** Read-only container log tail. Loads lazily; the container ref is re-validated in DockerService. */
class ContainerLogs extends ModalComponent
{
    public int $serverId;

    public string $ref;

    public string $logs = '';

    public bool $loaded = false;

    public ?string $error = null;

    public function mount(int $serverId, string $ref): void
    {
        $this->serverId = $serverId;
        $this->ref = $ref;
    }

    public static function modalMaxWidth(): string
    {
        return 'lg';
    }

    public function load(DockerService $docker): void
    {
        // Re-gate the actual read (not just the opener): logs can leak secrets → operate only.
        abort_unless(Auth::user()?->can('operate'), 403);

        // serverId 0 = the Clusev host (transient server from the host login); otherwise a fleet server.
        // The host is admin-only (manage-fleet), matching the host terminal + the Docker host target.
        if ($this->serverId === 0) {
            abort_unless(Auth::user()?->can('manage-fleet'), 403);
        }
        $server = $this->serverId === 0
            ? HostCredential::current()?->toServer()
            : Server::find($this->serverId);
        if (! $server) {
            $this->error = __('common.server_not_found');
            $this->loaded = true;

            return;
        }

        try {
            $this->logs = $docker->logs($server, $this->ref, 200);
        } catch (InvalidArgumentException) {
            $this->error = __('docker.invalid_ref');
        } catch (Throwable $e) {
            $this->error = $e->getMessage();
        }

        $this->loaded = true;
    }

    public function render()
    {
        return view('livewire.modals.container-logs');
    }
}
