<?php

namespace Tests\Unit;

use App\Models\Tenant;
use App\Support\TenantRequestResolver;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Request;
use Tests\TestCase;

class TenantRequestResolverTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_returns_tenant_from_request_attribute(): void
    {
        $tenant = Tenant::factory()->make();
        $request = Request::create('/api/tenant/test', 'GET');
        $request->attributes->set('tenant', $tenant);

        $resolved = TenantRequestResolver::resolve($request);

        $this->assertSame($tenant, $resolved);
    }

    public function test_it_finds_tenant_using_identifier(): void
    {
        $tenant = Tenant::factory()->create();
        $request = Request::create('/api/tenant/test', 'GET');
        $request->attributes->set('tenant_id', $tenant->id);

        $resolved = TenantRequestResolver::resolve($request);

        $this->assertTrue($tenant->is($resolved));
    }

    public function test_it_throws_when_tenant_cannot_be_resolved(): void
    {
        $this->expectException(HttpResponseException::class);

        $request = Request::create('/api/tenant/test', 'GET');

        TenantRequestResolver::resolve($request);
    }
}
