<?php

namespace Tests\Feature\Tenant;

use App\Http\Controllers\Api\TenantPackageController;
use App\Models\Event;
use App\Models\EventPackage;
use App\Models\Package;
use App\Models\TenantPackage;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class TenantPackageOverviewTest extends TenantTestCase
{
    public function test_active_package_contains_limits_and_features(): void
    {
        Carbon::setTestNow(Carbon::parse('2024-01-01 10:00:00'));

        $package = Package::factory()->reseller()->create([
            'max_photos' => 1500,
            'max_guests' => 250,
            'gallery_days' => 30,
            'max_tasks' => 40,
            'max_events_per_year' => 12,
            'features' => ['custom_branding', 'reseller_dashboard'],
            'branding_allowed' => true,
            'watermark_allowed' => false,
        ]);

        TenantPackage::factory()->create([
            'tenant_id' => $this->tenant->id,
            'package_id' => $package->id,
            'active' => true,
            'used_events' => 3,
        ]);

        $event = Event::factory()->create([
            'tenant_id' => $this->tenant->id,
        ]);

        EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => 199.00,
            'purchased_at' => now()->subDay(),
            'used_photos' => 700,
            'used_guests' => 120,
            'gallery_expires_at' => now()->addDays(20),
        ]);

        $request = Request::create('/api/v1/tenant/packages', 'GET');
        $request->attributes->set('tenant', $this->tenant);

        $response = app(TenantPackageController::class)->index($request);
        $payload = $response->getData(true);

        $this->assertSame(1500, $payload['active_package']['package_limits']['max_photos']);
        $this->assertSame(250, $payload['active_package']['package_limits']['max_guests']);
        $this->assertSame(30, $payload['active_package']['package_limits']['gallery_days']);
        $this->assertSame(40, $payload['active_package']['package_limits']['max_tasks']);
        $this->assertSame(12, $payload['active_package']['package_limits']['max_events_per_year']);
        $this->assertTrue($payload['active_package']['package_limits']['branding_allowed']);
        $this->assertFalse($payload['active_package']['package_limits']['watermark_allowed']);
        $this->assertSame(['custom_branding', 'reseller_dashboard'], $payload['active_package']['package_limits']['features']);
        $this->assertSame(700, $payload['active_package']['package_limits']['used_photos']);
        $this->assertSame(800, $payload['active_package']['package_limits']['remaining_photos']);
        $this->assertSame(120, $payload['active_package']['package_limits']['used_guests']);
        $this->assertSame(130, $payload['active_package']['package_limits']['remaining_guests']);
        $this->assertSame(20, $payload['active_package']['package_limits']['remaining_gallery_days']);
        $this->assertSame(10, $payload['active_package']['package_limits']['used_gallery_days']);
        $this->assertSame(9, $payload['active_package']['remaining_events']);
    }

    public function test_active_package_falls_back_to_active_endcustomer_package(): void
    {
        $package = Package::factory()->endcustomer()->create([
            'features' => ['custom_branding'],
            'branding_allowed' => true,
            'watermark_allowed' => false,
        ]);

        TenantPackage::factory()->create([
            'tenant_id' => $this->tenant->id,
            'package_id' => $package->id,
            'active' => true,
        ]);

        $request = Request::create('/api/v1/tenant/packages', 'GET');
        $request->attributes->set('tenant', $this->tenant);

        $response = app(TenantPackageController::class)->index($request);
        $payload = $response->getData(true);

        $this->assertSame($package->id, $payload['active_package']['package_id']);
        $this->assertSame(['custom_branding'], $payload['active_package']['package_limits']['features']);
    }
}
