<?php

use App\Models\User;

$tabs = ['dashboard', 'cloud', 'users', 'backups', 'invoices', 'support'];

it('redirects guests from portal tabs to login', function (string $tab) {
    $this->get(route($tab))->assertRedirect('/login');
})->with($tabs);

it('renders portal tabs for authenticated users', function (string $tab) {
    $user = User::factory()->create();

    $this->actingAs($user)->get(route($tab))
        ->assertOk()
        ->assertSee('CluPilot');
})->with($tabs);

it('binds the cloud card to the real instance', function () {
    $user = \App\Models\User::factory()->create(['email' => 'c@cloud.test']);
    $customer = \App\Models\Customer::factory()->create(['email' => 'c@cloud.test', 'user_id' => $user->id, 'name' => 'Acme AG']);
    $host = \App\Models\Host::factory()->active()->create();
    $instance = \App\Models\Instance::factory()->create([
        'customer_id' => $customer->id, 'host_id' => $host->id, 'status' => 'active',
        'plan' => 'business', 'subdomain' => 'acme-ag', 'quota_gb' => 1000,
    ]);
    // The card states what this customer bought, so there has to be a contract
    // to state it from — as there is for every running machine in production.
    \App\Models\Subscription::factory()->plan('business')->create([
        'customer_id' => $customer->id, 'instance_id' => $instance->id,
    ]);

    $this->actingAs($user)->get(route('cloud'))
        ->assertOk()
        // Through the same accessor the page uses. Reading config() directly
        // stopped matching when the zone moved into the settings table.
        ->assertSee('acme-ag.'.App\Support\ProvisioningSettings::dnsZone())   // real subdomain, not a fixture
        ->assertSee(__('billing.plan.business'))
        ->assertSee(__('billing.perf.high'));
});
