<?php

namespace App\Tenancy;

use App\Models\Central\Company;

/**
 * Tenant context resolver. Stored in the application container as a
 * singleton so middleware can set the active tenant, and Eloquent
 * scopes / Filament resources can read it.
 *
 * Usage:
 *   app(TenantManager::class)->setCurrent($company);
 *   tenant() // returns Company|null
 */
class TenantManager
{
    protected ?Company $current = null;

    public function setCurrent(?Company $company): void
    {
        $this->current = $company;
    }

    public function current(): ?Company
    {
        return $this->current;
    }

    public function currentId(): ?int
    {
        return $this->current?->id;
    }

    public function isResolved(): bool
    {
        return $this->current !== null;
    }

    public function clear(): void
    {
        $this->current = null;
    }
}
