<?php

namespace Tests\Feature;

use App\Models\Central\Company;
use App\Models\Central\Plan;
use App\Models\Tenant\Client;
use App\Models\Tenant\TireSet;
use App\Models\Tenant\TireStorage;
use App\Tenancy\TenantManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class TireServiceTest extends TestCase
{
    use RefreshDatabase;

    private Company $company;

    protected function setUp(): void
    {
        parent::setUp();
        $this->company = $this->makeCompany('tires');
    }

    public function test_size_label_formats_correctly(): void
    {
        $set = $this->makeSet(['width' => 205, 'profile' => 55, 'diameter' => 16]);
        $this->assertEquals('205/55 R16', $set->sizeLabel());

        $incomplete = $this->makeSet(['width' => 205, 'profile' => null, 'diameter' => 16]);
        $this->assertEquals('—', $incomplete->sizeLabel());
    }

    public function test_tread_min_computed_on_save(): void
    {
        $set = $this->makeSet(['tread' => ['fl' => 6.5, 'fr' => 6.0, 'rl' => 4.2, 'rr' => 4.8]]);
        $this->assertEquals(4.2, (float) $set->tread_min);
    }

    public function test_check_in_creates_active_storage(): void
    {
        $set = $this->makeSet();
        TireStorage::create([
            'tire_set_id' => $set->id,
            'location' => 'A1-03',
            'season_label' => 'Iarnă 2025-2026',
            'fee' => 500,
            'status' => 'stored',
            'checked_in_at' => now(),
        ]);

        $this->assertTrue($set->fresh()->isStored());
        $this->assertEquals('A1-03', $set->fresh()->currentStorage()->location);
    }

    public function test_check_out_marks_retrieved(): void
    {
        $set = $this->makeSet();
        $storage = TireStorage::create([
            'tire_set_id' => $set->id, 'location' => 'B2', 'status' => 'stored', 'checked_in_at' => now()->subMonths(5),
        ]);

        $storage->update(['status' => 'retrieved', 'checked_out_at' => now()]);

        $this->assertFalse($set->fresh()->isStored());
        $this->assertEquals('retrieved', $storage->fresh()->status);
        $this->assertGreaterThanOrEqual(140, $storage->fresh()->durationDays());
    }

    public function test_set_can_have_multiple_storage_stays(): void
    {
        $set = $this->makeSet();
        // Past stay (retrieved) + current stay (stored).
        TireStorage::create(['tire_set_id' => $set->id, 'location' => 'A1', 'status' => 'retrieved', 'checked_in_at' => now()->subYear(), 'checked_out_at' => now()->subMonths(6)]);
        TireStorage::create(['tire_set_id' => $set->id, 'location' => 'A2', 'status' => 'stored', 'checked_in_at' => now()]);

        $this->assertEquals(2, $set->storage()->count());
        $this->assertEquals('A2', $set->currentStorage()->location);
    }

    public function test_tire_sets_isolated_per_tenant(): void
    {
        $this->makeSet();
        $other = $this->makeCompany('othertires');
        app(TenantManager::class)->setCurrent($other);
        $this->assertEquals(0, TireSet::count());
    }

    private function makeCompany(string $slug): Company
    {
        $plan = Plan::firstOrCreate(['slug' => 'test'], ['name' => 'T', 'price' => 0, 'features' => []]);
        $company = Company::create(['plan_id' => $plan->id, 'slug' => $slug, 'name' => ucfirst($slug), 'status' => 'active']);
        app(TenantManager::class)->setCurrent($company);
        return $company;
    }

    private function makeSet(array $attrs = []): TireSet
    {
        $client = Client::create(['name' => 'C', 'phone' => '+3736' . random_int(1000000, 9999999), 'type' => 'individual', 'status' => 'active']);
        return TireSet::create(array_merge([
            'client_id' => $client->id,
            'season' => 'winter',
            'brand' => 'Michelin',
        ], $attrs));
    }
}
