<?php

namespace Tests\Feature\Console;

use App\Console\Commands\CheckEventPackages;
use App\Events\Packages\EventPackageGalleryExpired;
use App\Events\Packages\EventPackageGalleryExpiring;
use App\Events\Packages\TenantPackageExpired;
use App\Events\Packages\TenantPackageExpiring;
use App\Models\Event;
use App\Models\EventPackage;
use App\Models\Package;
use App\Models\Tenant;
use App\Models\TenantPackage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event as EventFacade;
use Tests\TestCase;

class CheckEventPackagesCommandTest extends TestCase
{
    use RefreshDatabase;

    public function test_dispatches_gallery_warning_and_updates_timestamp(): void
    {
        EventFacade::fake();

        $tenant = Tenant::factory()->create();
        $package = Package::factory()->endcustomer()->create([
            'max_photos' => 100,
        ]);
        $event = Event::factory()->for($tenant)->create();

        $eventPackage = EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now()->subMonth(),
            'used_photos' => 10,
            'used_guests' => 0,
            'gallery_expires_at' => now()->copy()->addDays(7),
        ]);

        Artisan::call(CheckEventPackages::class);

        EventFacade::assertDispatched(EventPackageGalleryExpiring::class, function ($event) use ($eventPackage) {
            return $event->eventPackage->is($eventPackage) && $event->daysRemaining === 7;
        });

        $this->assertNotNull($eventPackage->fresh()->gallery_warning_sent_at);
    }

    public function test_dispatches_gallery_expired_and_updates_timestamp(): void
    {
        EventFacade::fake();

        $tenant = Tenant::factory()->create();
        $package = Package::factory()->endcustomer()->create([
            'max_photos' => 100,
        ]);
        $event = Event::factory()->for($tenant)->create();

        $eventPackage = EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now()->subMonth(),
            'used_photos' => 10,
            'used_guests' => 0,
            'gallery_expires_at' => now()->copy()->subDay(),
        ]);

        Artisan::call(CheckEventPackages::class);

        EventFacade::assertDispatched(EventPackageGalleryExpired::class, function ($event) use ($eventPackage) {
            return $event->eventPackage->is($eventPackage);
        });

        $this->assertNotNull($eventPackage->fresh()->gallery_expired_notified_at);
    }

    public function test_dispatches_tenant_package_expiry_warning_and_updates_timestamp(): void
    {
        EventFacade::fake();

        Config::set('package-limits.package_expiry_days', [6, 1]);

        $now = now()->startOfMinute();
        Carbon::setTestNow($now);

        try {
            $tenant = Tenant::factory()->create();
            $package = Package::factory()->reseller()->create([
                'max_events_per_year' => 5,
            ]);

            $tenantPackage = TenantPackage::factory()->for($tenant)->for($package)->create([
                'expires_at' => $now->copy()->addDays(6),
                'expiry_warning_sent_at' => null,
            ]);

            Artisan::call(CheckEventPackages::class);

            EventFacade::assertDispatched(TenantPackageExpiring::class, function ($event) use ($tenantPackage) {
                return $event->tenantPackage->is($tenantPackage) && $event->daysRemaining === 6;
            });

            $this->assertNotNull($tenantPackage->fresh()->expiry_warning_sent_at);
        } finally {
            Carbon::setTestNow();
        }
    }

    public function test_dispatches_tenant_package_expired_and_updates_timestamp(): void
    {
        EventFacade::fake();

        $now = now()->startOfMinute();
        Carbon::setTestNow($now);

        try {
            $tenant = Tenant::factory()->create();
            $package = Package::factory()->reseller()->create([
                'max_events_per_year' => 5,
            ]);

            $tenantPackage = TenantPackage::factory()->for($tenant)->for($package)->create([
                'expires_at' => $now->copy()->subDay(),
                'expired_notified_at' => null,
            ]);

            Artisan::call(CheckEventPackages::class);

            EventFacade::assertDispatched(TenantPackageExpired::class, function ($event) use ($tenantPackage) {
                return $event->tenantPackage->is($tenantPackage);
            });

            $this->assertNotNull($tenantPackage->fresh()->expired_notified_at);
        } finally {
            Carbon::setTestNow();
        }
    }

}
