<?php

namespace App\Services;

use App\Models\Company;
use Illuminate\Support\Facades\Auth;

class CompanyResolver
{
    protected ?Company $company = null;

    public function getCompany(): ?Company
    {
        if ($this->company !== null) {
            return $this->company;
        }

        $user = Auth::user();
        if ($user && $user->company_id) {
            $this->company = Company::withoutGlobalScopes()->find($user->company_id);
        }

        return $this->company;
    }

    public function getCompanyId(): ?int
    {
        $company = $this->getCompany();
        return $company?->id;
    }

    public function setCompany(?Company $company): self
    {
        $this->company = $company;
        return $this;
    }

    public function clear(): self
    {
        $this->company = null;
        return $this;
    }
}
