<?php

namespace Tests\Feature\Mexco;

use App\Models\MexcoAddon;
use App\Models\MexcoCheckoutOrder;
use App\Models\MexcoEventAddon;
use App\Models\MexcoInvoice;
use App\Models\MexcoOrganisationUser;
use App\Models\MeetupEvent;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Concerns\SetsUpMexcoPlatform;
use Tests\TestCase;

class CustomerPortalPhase4Test extends TestCase
{
    use RefreshDatabase;
    use SetsUpMexcoPlatform;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seedMexcoPlatform();
    }

    public function test_customer_can_add_gamification_review_and_submit_for_payment(): void
    {
        $this->post(route('mexco.register.submit'), [
            'name' => 'Siti Aminah',
            'email' => 'siti@umpsa.edu.my',
            'password' => 'password123',
            'password_confirmation' => 'password123',
            'organisation_name' => 'UMPSA',
            'organisation_type' => 'university',
            'plan' => 'starter',
        ])->assertRedirect(route('mexco.checkout.event'));

        $user = MexcoOrganisationUser::query()->where('email', 'siti@umpsa.edu.my')->firstOrFail();
        $orderId = MexcoCheckoutOrder::query()->latest('id')->value('id');

        $session = [
            'mexco_portal_authenticated' => true,
            'mexco_portal_user_id' => $user->id,
            'mexco_portal_organisation_id' => $user->organisation_id,
            'mexco_portal_user_name' => $user->name,
            'mexco_portal_user_role' => $user->role,
            'mexco_checkout_order_id' => $orderId,
        ];

        $this->withSession($session)->post(route('mexco.checkout.event.store'), [
            'name' => 'ICAIIA2026',
            'expected_participants' => 80,
            'sessions_count' => 8,
            'booths_count' => 5,
            'event_type' => 'conference',
            'organisation_name' => 'UMPSA',
            'organisation_type' => 'university',
        ])->assertRedirect(route('mexco.checkout.addons'));

        $event = MeetupEvent::query()->where('name', 'ICAIIA2026')->firstOrFail();
        $gamification = MexcoAddon::query()->where('code', 'gamification')->firstOrFail();

        $this->withSession($session)->post(route('mexco.checkout.addons.store'), [
            'addon_ids' => [$gamification->id],
        ])->assertRedirect(route('mexco.checkout.review'));

        $this->assertDatabaseHas('mexco_event_addons', [
            'event_license_id' => $event->license->id,
            'addon_id' => $gamification->id,
            'status' => 'pending',
        ]);

        $order = MexcoCheckoutOrder::query()->findOrFail($orderId);
        $this->assertContains($gamification->id, $order->addon_ids);
        $this->assertSame(19900 + 19900, $order->subtotal_cents);

        $this->withSession($session)->post(route('mexco.checkout.review.submit'))
            ->assertRedirect(route('mexco.checkout.payment'));

        $order->refresh();
        $this->assertSame(MexcoCheckoutOrder::STATUS_PENDING_PAYMENT, $order->status);
        $this->assertSame(MexcoInvoice::STATUS_PENDING, $order->invoice?->status);
        $this->assertSame('pending', $event->fresh()->license?->status);

        $this->assertTrue(
            MexcoEventAddon::query()
                ->where('event_license_id', $event->license->id)
                ->where('addon_id', $gamification->id)
                ->exists()
        );

        $this->withSession($session)->get(route('mexco.portal.dashboard'))
            ->assertOk()
            ->assertSee('ICAIIA2026')
            ->assertSee('Gamification');
    }

    public function test_addons_page_only_lists_plan_eligible_addons(): void
    {
        $this->post(route('mexco.register.submit'), [
            'name' => 'Ali',
            'email' => 'ali@example.com',
            'password' => 'password123',
            'password_confirmation' => 'password123',
            'organisation_name' => 'Demo Org',
            'organisation_type' => 'corporate',
            'plan' => 'pro',
        ]);

        $user = MexcoOrganisationUser::query()->where('email', 'ali@example.com')->firstOrFail();
        $orderId = MexcoCheckoutOrder::query()->latest('id')->value('id');
        $session = [
            'mexco_portal_authenticated' => true,
            'mexco_portal_user_id' => $user->id,
            'mexco_portal_organisation_id' => $user->organisation_id,
            'mexco_portal_user_name' => $user->name,
            'mexco_portal_user_role' => $user->role,
            'mexco_checkout_order_id' => $orderId,
        ];

        $this->withSession($session)->post(route('mexco.checkout.event.store'), [
            'name' => 'Pro Event',
            'expected_participants' => 200,
        ]);

        $response = $this->withSession($session)->get(route('mexco.checkout.addons'));
        $response->assertOk();
        $response->assertSee('WhatsApp Notification');
        $response->assertDontSee('Gamification');
        $response->assertDontSee('Floor Plan');
        $response->assertDontSee('Booth Voting');
        $response->assertDontSee('Custom Certificate');
    }
}
