<?php

use App\Livewire\Billing;
use App\Livewire\Cloud;
use App\Models\Customer;
use App\Models\Host;
use App\Models\Instance;
use App\Models\Operator;
use App\Models\Subscription;
use App\Models\SubscriptionAddon;
use App\Models\User;
use Livewire\Livewire;

/**
 * The owner's third decision: the portal shows a granted package without a
 * price — not "free", not struck through — so a later conversion to paid
 * does not read as a negotiation.
 */
function customerWithGrantedPlan(): array
{
    $customer = Customer::factory()->create();
    $user = User::factory()->create(['email' => $customer->email]);
    $customer->update(['user_id' => $user->id]);

    $instance = Instance::factory()->create([
        'customer_id' => $customer->id,
        'host_id' => Host::factory()->active()->create()->id,
        'plan' => 'team',
        'status' => 'active',
    ]);
    $subscription = Subscription::factory()->plan('team')->create([
        'customer_id' => $customer->id,
        'instance_id' => $instance->id,
        'price_cents' => 0,
        'granted_by' => Operator::factory()->create()->id,
        'granted_at' => now(),
        'catalogue_price_cents' => 17900,
    ]);

    return [$customer, $user, $subscription];
}

it('shows the granted plan on the cloud dashboard without a price', function () {
    [, $user] = customerWithGrantedPlan();

    Livewire::actingAs($user)
        ->test(Cloud::class)
        // The formatted amount, not the bare digits: "179" also matches an id,
        // and ids climb through a suite run because a rolled-back transaction
        // does not reset an auto-increment counter. That made this test pass
        // alone and fail in company. The line below is what a leak would
        // actually look like on the page.
        ->assertDontSee('179,00')
        ->assertDontSee('0 €')
        ->assertSee(__('billing.plan.team'));
});

it('shows the granted plan on the billing page without a price', function () {
    [, $user] = customerWithGrantedPlan();

    $card = Livewire::actingAs($user)->test(Billing::class)->viewData('current');

    expect($card['granted'])->toBeTrue();

    Livewire::actingAs($user)
        ->test(Billing::class)
        ->assertSee(__('billing.granted_plan'))
        ->assertDontSee('179,00');
});

it('shows a granted module without a price, but a booked one still with its price', function () {
    [$customer, $user, $subscription] = customerWithGrantedPlan();

    SubscriptionAddon::query()->create([
        'subscription_id' => $subscription->id,
        'addon_key' => 'priority_support',
        'price_cents' => 0,
        'currency' => 'EUR',
        'quantity' => 1,
        'booked_at' => now(),
        'granted_by' => Operator::factory()->create()->id,
        'granted_at' => now(),
        'catalogue_price_cents' => 2900,
    ]);

    Livewire::actingAs($user)
        ->test(Billing::class)
        ->assertSee(__('billing.addon.priority_support.name'))
        ->assertSee(__('billing.granted_plan'));
});
