<?php

use App\Enums\CreditProductType;
use App\Models\CreditProduct;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;

uses(RefreshDatabase::class);

it('lists active credit products ordered for the mobile app', function () {
    Sanctum::actingAs(User::factory()->create());

    CreditProduct::create([
        'name' => 'Pack 10',
        'type' => CreditProductType::ONE_TIME,
        'credits' => 10,
        'amount' => 499,
        'currency' => 'eur',
        'stripe_price_id' => 'price_pack_10',
        'sort_order' => 20,
    ]);
    CreditProduct::create([
        'name' => 'Mensuel 30',
        'type' => CreditProductType::MONTHLY,
        'credits' => 30,
        'amount' => 999,
        'currency' => 'eur',
        'stripe_price_id' => 'price_monthly_30',
        'sort_order' => 10,
    ]);
    CreditProduct::create([
        'name' => 'Inactive',
        'type' => CreditProductType::ONE_TIME,
        'credits' => 5,
        'amount' => 299,
        'currency' => 'eur',
        'stripe_price_id' => 'price_inactive',
        'is_active' => false,
    ]);

    $this
        ->getJson('/api/billing/products')
        ->assertOk()
        ->assertJsonCount(2, 'data')
        ->assertJsonPath('data.0.name', 'Mensuel 30')
        ->assertJsonPath('data.0.type', CreditProductType::MONTHLY->value)
        ->assertJsonPath('data.0.credits', 30)
        ->assertJsonPath('data.0.formattedAmount', '9,99 EUR')
        ->assertJsonPath('data.1.name', 'Pack 10');
});
