<?php

namespace Tests\Feature;

use App\Models\Event;
use App\Models\EventPackage;
use App\Models\EventPackageAddon;
use App\Models\GiftVoucher;
use App\Models\Package;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CustomerEmailRenderTest extends TestCase
{
    use RefreshDatabase;

    public function test_customer_emails_render_in_de(): void
    {
        app()->setLocale('de');

        [$user, $tenant, $package] = $this->makeCustomerContext();

        $welcome = view('emails.welcome', ['user' => $user])->render();
        $this->assertStringContainsString('Die Fotospiel.App', $welcome);

        $abandoned = view('emails.abandoned-checkout', [
            'user' => $user,
            'package' => $package,
            'packageName' => $package->name,
            'timing' => '1h',
            'resumeUrl' => 'https://example.test/checkout',
        ])->render();
        $this->assertStringContainsString($package->name, $abandoned);

        $contact = view('emails.contact-confirmation', ['name' => 'Soren'])->render();
        $this->assertStringContainsString('Soren', $contact);

        $event = Event::factory()->create(['tenant_id' => $tenant->id]);
        $eventPackage = EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => 59,
            'purchased_at' => now(),
        ]);
        $addon = EventPackageAddon::create([
            'event_package_id' => $eventPackage->id,
            'event_id' => $event->id,
            'tenant_id' => $tenant->id,
            'addon_key' => 'extra_photos',
            'extra_photos' => 250,
            'amount' => 9.0,
            'currency' => 'EUR',
            'status' => 'completed',
            'metadata' => ['label' => 'Extra Fotos'],
            'purchased_at' => now(),
        ]);
        $receipt = view('emails.addons.receipt', ['addon' => $addon])->render();
        $this->assertStringContainsString('Extra Fotos', $receipt);

        $voucher = GiftVoucher::factory()->create([
            'message' => 'Herzlichen Glückwunsch!',
        ]);
        $gift = view('emails.gift-voucher', [
            'voucher' => $voucher,
            'amount' => number_format((float) $voucher->amount, 2),
            'currency' => $voucher->currency,
            'forRecipient' => false,
            'printUrl' => 'https://example.test/print',
        ])->render();
        $this->assertStringContainsString($voucher->code, $gift);
    }

    public function test_customer_emails_render_in_en(): void
    {
        app()->setLocale('en');

        [$user, $tenant, $package] = $this->makeCustomerContext();

        $welcome = view('emails.welcome', ['user' => $user])->render();
        $this->assertStringContainsString('Die Fotospiel.App', $welcome);

        $abandoned = view('emails.abandoned-checkout', [
            'user' => $user,
            'package' => $package,
            'packageName' => $package->name,
            'timing' => '24h',
            'resumeUrl' => 'https://example.test/checkout',
        ])->render();
        $this->assertStringContainsString('Resume checkout', $abandoned);
    }

    /**
     * @return array{0: User, 1: Tenant, 2: Package}
     */
    private function makeCustomerContext(): array
    {
        $user = User::factory()->create([
            'first_name' => 'Soren',
            'last_name' => 'Eberhardt',
            'username' => 'soren',
        ]);
        $tenant = Tenant::factory()->create([
            'user_id' => $user->id,
        ]);
        $user->forceFill(['tenant_id' => $tenant->id])->save();

        $package = Package::factory()->create([
            'name' => 'Standard',
            'type' => 'endcustomer',
            'max_photos' => 500,
            'max_guests' => 200,
            'gallery_days' => 30,
        ]);

        return [$user, $tenant, $package];
    }
}
