<?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' => null,
            'active' => true,
        ]);

        $tenantPackage->refresh();

        $this->assertTrue($tenantPackage->isActive());
        $this->assertTrue($tenantPackage->active);
        $this->assertNotNull($tenantPackage->expires_at);
        $this->assertTrue($tenantPackage->expires_at->isFuture());
        $this->assertTrue($tenantPackage->expires_at->lessThanOrEqualTo(now()->addYears(2)));
    }

    public function test_reseller_packages_do_not_expire_by_default_but_can_be_expired(): 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->assertNull($tenantPackage->expires_at);
        $this->assertTrue($tenantPackage->isActive());

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

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