<?php

namespace Tests\Feature;

use App\Models\Plan;
use App\Models\User;
use Livewire\Livewire;
use Tests\TestCase;

class PricingCheckoutTest extends TestCase
{
    /** @test */
    public function it_renders_pricing_component_with_working_checkout_buttons()
    {
        $user = User::factory()->create();
        $plan = Plan::factory()->create([
            'name' => 'Test Plan',
            'price' => 99.99,
            'monthly_billing' => true,
        ]);

        // Enable polar provider for this plan
        $plan->planProviders()->create([
            'provider' => 'polar',
            'is_enabled' => true,
        ]);

        Livewire::actingAs($user)
            ->test(\App\Livewire\Dashboard\Pricing::class)
            ->assertSet('plans')
            ->assertSet('planTiers')
            ->assertSee($plan->name);
    }

    /** @test */
    public function it_can_redirect_to_enhanced_checkout_for_polar_provider()
    {
        $user = User::factory()->create();
        $plan = Plan::factory()->create([
            'name' => 'Test Plan',
            'price' => 99.99,
            'monthly_billing' => true,
        ]);

        // Enable polar provider for this plan
        $plan->planProviders()->create([
            'provider' => 'polar',
            'is_enabled' => true,
        ]);

        Livewire::actingAs($user)
            ->test(\App\Livewire\Dashboard\Pricing::class)
            ->call('choosePlan', $plan->id, 'polar')
            ->assertRedirect(route('checkout.enhanced', [
                'plan' => $plan->id,
                'provider' => 'polar',
            ]));
    }

    /** @test */
    public function it_can_redirect_to_trial_checkout_when_plan_supports_trials()
    {
        $user = User::factory()->create();
        $plan = Plan::factory()->create([
            'name' => 'Test Plan with Trial',
            'price' => 99.99,
            'monthly_billing' => true,
        ]);

        // Enable polar provider for this plan
        $plan->planProviders()->create([
            'provider' => 'polar',
            'is_enabled' => true,
        ]);

        // Create trial configuration
        $plan->trialConfiguration()->create([
            'trial_duration_days' => 14,
            'trial_requires_payment_method' => true,
            'trial_auto_converts' => true,
        ]);

        Livewire::actingAs($user)
            ->test(\App\Livewire\Dashboard\Pricing::class)
            ->call('startTrial', $plan->id, 'polar')
            ->assertRedirect(route('checkout.trial', [
                'plan' => $plan->id,
                'provider' => 'polar',
            ]));
    }

    /** @test */
    public function it_shows_error_when_provider_is_not_supported()
    {
        $user = User::factory()->create();
        $plan = Plan::factory()->create();

        Livewire::actingAs($user)
            ->test(\App\Livewire\Dashboard\Pricing::class)
            ->call('choosePlan', $plan->id, 'unsupported_provider')
            ->assertSessionHas('error', "This plan doesn't support unsupported_provider payments.");
    }

    /** @test */
    public function it_shows_error_when_trying_trial_on_plan_without_trial()
    {
        $user = User::factory()->create();
        $plan = Plan::factory()->create();

        // Enable provider but no trial configuration
        $plan->planProviders()->create([
            'provider' => 'polar',
            'is_enabled' => true,
        ]);

        Livewire::actingAs($user)
            ->test(\App\Livewire\Dashboard\Pricing::class)
            ->call('startTrial', $plan->id, 'polar')
            ->assertSessionHas('error', "This plan doesn't offer trials.");
    }
}
