<?php

namespace Tests\Unit;

use App\Models\Package;
use App\Models\TenantPackage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class TenantPackageTest extends TestCase
{
    use RefreshDatabase;

    public function test_endcustomer_packages_remain_active_without_expiry(): void
    {
        $package = Package::factory()->endcustomer()->create();

        $tenantPackage = TenantPackage::factory()->create([
            'package_id' => $package->id,
            'expires_at' => now()->subYear(),
            'active' => true,
        ]);

        $tenantPackage->refresh();

        $this->assertTrue($tenantPackage->isActive());
        $this->assertTrue($tenantPackage->active);
        $this->assertTrue($tenantPackage->expires_at->greaterThan(now()->addYears(50)));
    }

    public function test_reseller_packages_still_expire(): void
    {
        $package = Package::factory()->reseller()->create(['max_events_per_year' => 5]);

        $tenantPackage = TenantPackage::factory()->create([
            'package_id' => $package->id,
            'expires_at' => null,
        ]);

        $tenantPackage->refresh();

        $this->assertNotNull($tenantPackage->expires_at);
        $this->assertTrue($tenantPackage->expires_at->isFuture());

        $tenantPackage->forceFill(['expires_at' => now()->subDay()])->save();

        $this->assertFalse($tenantPackage->fresh()->isActive());
        $this->assertFalse($tenantPackage->fresh()->active);
    }
}
