<?php

namespace Tests\Feature\Marketing;

use App\Models\Event;
use App\Models\EventPackage;
use App\Models\Package;
use App\Models\PackagePurchase;
use App\Models\Tenant;
use App\Models\TenantPackage;
use App\Models\User;
use App\Notifications\Customer\WithdrawalConfirmed;
use App\Services\Paddle\PaddleTransactionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;

class WithdrawalConfirmationTest extends TestCase
{
    use RefreshDatabase;

    public function test_withdrawal_page_lists_eligible_purchases(): void
    {
        $user = User::factory()->create();
        $tenant = Tenant::factory()->create(['user_id' => $user->id]);
        $user->forceFill(['tenant_id' => $tenant->id])->save();

        $package = Package::factory()->create(['type' => 'endcustomer']);
        $purchase = PackagePurchase::factory()->create([
            'tenant_id' => $tenant->id,
            'package_id' => $package->id,
            'provider' => 'paddle',
            'provider_id' => 'txn_123',
            'refunded' => false,
            'type' => 'endcustomer_event',
            'purchased_at' => now()->subDays(2),
        ]);

        $response = $this->actingAs($user)->get('/de/widerruf');

        $response->assertOk();
        $response->assertInertia(fn (Assert $page) => $page
            ->component('marketing/WithdrawalConfirm')
            ->where('windowDays', 14)
            ->has('eligiblePurchases', 1)
            ->where('eligiblePurchases.0.id', $purchase->id)
        );
    }

    public function test_withdrawal_confirmation_refunds_and_sends_email(): void
    {
        Notification::fake();

        $user = User::factory()->create();
        $tenant = Tenant::factory()->create(['user_id' => $user->id]);
        $user->forceFill(['tenant_id' => $tenant->id])->save();

        $package = Package::factory()->create(['type' => 'endcustomer']);
        $purchase = PackagePurchase::factory()->create([
            'tenant_id' => $tenant->id,
            'package_id' => $package->id,
            'provider' => 'paddle',
            'provider_id' => 'txn_456',
            'refunded' => false,
            'type' => 'endcustomer_event',
            'purchased_at' => now()->subDays(5),
        ]);

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

        $this->mock(PaddleTransactionService::class, function ($mock) {
            $mock->shouldReceive('refund')
                ->once()
                ->andReturn([]);
        });

        $response = $this->actingAs($user)->post('/de/widerruf', [
            'purchase_id' => $purchase->id,
        ]);

        $response->assertSessionHas('success');
        $this->assertTrue($purchase->fresh()->refunded);
        $this->assertFalse($tenantPackage->fresh()->active);

        Notification::assertSentOnDemand(
            WithdrawalConfirmed::class,
            function (WithdrawalConfirmed $notification, array $channels) {
                return in_array('mail', $channels, true);
            }
        );
    }

    public function test_withdrawal_rejected_when_event_exists(): void
    {
        $user = User::factory()->create();
        $tenant = Tenant::factory()->create(['user_id' => $user->id]);
        $user->forceFill(['tenant_id' => $tenant->id])->save();

        $package = Package::factory()->create(['type' => 'endcustomer']);
        $purchase = PackagePurchase::factory()->create([
            'tenant_id' => $tenant->id,
            'package_id' => $package->id,
            'provider' => 'paddle',
            'provider_id' => 'txn_789',
            'refunded' => false,
            'type' => 'endcustomer_event',
            'purchased_at' => now()->subDays(3),
        ]);

        $event = Event::factory()->create(['tenant_id' => $tenant->id]);
        EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now(),
        ]);

        $this->mock(PaddleTransactionService::class, function ($mock) {
            $mock->shouldReceive('refund')->never();
        });

        $response = $this->actingAs($user)->post('/de/widerruf', [
            'purchase_id' => $purchase->id,
        ]);

        $response->assertSessionHas('error');
        $this->assertFalse($purchase->fresh()->refunded);
    }
}
