<?php

declare(strict_types=1);

namespace App\Support\Tenants;

use App\Models\ManagedEnvironment;

final readonly class ReferencedTenantLifecyclePresentation
{
    public function __construct(
        public string $viewerContext,
        public ?int $tenantId,
        public ?string $tenantName,
        public TenantLifecyclePresentation $presentation,
        public bool $removedFromWorkspace,
        public bool $workspaceClosed,
        public ?string $contextNote,
    ) {}

    public static function forOperationRun(ManagedEnvironment $tenant): self
    {
        return self::fromTenant($tenant, 'operation_run');
    }

    public static function fromTenant(ManagedEnvironment $tenant, string $viewerContext): self
    {
        $presentation = TenantLifecyclePresentation::fromTenant($tenant);

        return new self(
            viewerContext: $viewerContext,
            tenantId: (int) $tenant->getKey(),
            tenantName: $tenant->name,
            presentation: $presentation,
            removedFromWorkspace: $tenant->isRemovedFromWorkspace(),
            workspaceClosed: (bool) $tenant->workspace?->isClosed(),
            contextNote: self::contextNoteFor($presentation, $tenant->isRemovedFromWorkspace(), (bool) $tenant->workspace?->isClosed()),
        );
    }

    public static function forInvalid(string $viewerContext, ?ManagedEnvironment $tenant = null, ?string $normalizedValue = null): self
    {
        return new self(
            viewerContext: $viewerContext,
            tenantId: $tenant instanceof ManagedEnvironment ? (int) $tenant->getKey() : null,
            tenantName: $tenant?->name,
            presentation: TenantLifecyclePresentation::invalid($normalizedValue),
            removedFromWorkspace: $tenant?->isRemovedFromWorkspace() ?? false,
            workspaceClosed: (bool) $tenant?->workspace?->isClosed(),
            contextNote: 'Some tenant follow-up actions may be unavailable from this canonical workspace view.',
        );
    }

    public function selectorAvailabilityMessage(): ?string
    {
        if ($this->removedFromWorkspace) {
            return 'This tenant was removed from its workspace and may not appear in the tenant selector.';
        }

        if ($this->workspaceClosed) {
            return 'This tenant belongs to a closed workspace and may not appear in active tenant context.';
        }

        if ($this->presentation->isInvalidFallback) {
            return 'This tenant has an invalid lifecycle value and may not appear in the tenant selector.';
        }

        if (! $this->presentation->isSelectableAsContext()) {
            return 'This tenant is currently '.$this->presentation->lowercaseLabel().' and may not appear in the tenant selector.';
        }

        return null;
    }

    private static function contextNoteFor(TenantLifecyclePresentation $presentation, bool $removedFromWorkspace, bool $workspaceClosed): ?string
    {
        if ($removedFromWorkspace || $workspaceClosed) {
            return 'Historical operation context remains available, but active tenant follow-up actions are unavailable from this canonical workspace view.';
        }

        if ($presentation->isInvalidFallback || ! $presentation->isSelectableAsContext()) {
            return 'Some tenant follow-up actions may be unavailable from this canonical workspace view.';
        }

        return null;
    }
}
