<?php

declare(strict_types=1);

use App\Models\Plan;
use App\Models\PlanPrice;
use App\Models\User;

beforeEach(function () {
    $this->plan = Plan::factory()->create([
        'slug' => 'vps-test',
        'service_type' => 'vps',
        'price' => 15.00,
        'status' => 'active',
        'features' => ['cpu' => '2 vCPU', 'ram' => '4 GB'],
    ]);

    PlanPrice::create(['plan_id' => $this->plan->id, 'billing_cycle' => 'monthly', 'price' => 15.00]);
    PlanPrice::create(['plan_id' => $this->plan->id, 'billing_cycle' => 'quarterly', 'price' => 42.75]);
    PlanPrice::create(['plan_id' => $this->plan->id, 'billing_cycle' => 'semi_annual', 'price' => 81.00]);
    PlanPrice::create(['plan_id' => $this->plan->id, 'billing_cycle' => 'annual', 'price' => 153.00]);
});

it('loads checkout page with plan prices', function () {
    $user = User::factory()->create();

    $response = $this->actingAs($user)
        ->get("http://account.ezscale.dev/checkout/{$this->plan->id}");

    $response->assertOk();
    $response->assertInertia(fn ($page) => $page
        ->component('Checkout/Show')
        ->has('plan.prices', 4)
    );
});

it('validates billing_cycle on checkout', function () {
    $user = User::factory()->create();

    $response = $this->actingAs($user)
        ->post("http://account.ezscale.dev/checkout/{$this->plan->id}", [
            'gateway' => 'stripe',
            'billing_cycle' => 'weekly',
        ]);

    $response->assertSessionHasErrors('billing_cycle');
});

it('accepts valid billing cycles', function (string $cycle) {
    $user = User::factory()->create();

    $response = $this->actingAs($user)
        ->post("http://account.ezscale.dev/checkout/{$this->plan->id}", [
            'gateway' => 'stripe',
            'billing_cycle' => $cycle,
        ]);

    $response->assertSessionDoesntHaveErrors('billing_cycle');
})->with(['monthly', 'quarterly', 'semi_annual', 'annual']);
