<?php

use App\Livewire\CustomerProvisioning;
use App\Models\Customer;
use App\Models\Operator;
use App\Models\Order;
use App\Models\ProvisioningRun;
use App\Models\User;
use Livewire\Livewire;

it('shows real provisioning runs to admins', function () {
    $order = Order::factory()->create();
    ProvisioningRun::factory()->create([
        'subject_type' => Order::class, 'subject_id' => $order->id,
        'pipeline' => 'customer', 'status' => 'running', 'current_step' => 7,
    ]);

    $this->actingAs(Operator::factory()->role('Owner')->create(), 'operator')
        ->get(route('admin.provisioning'))
        ->assertOk()
        ->assertSee($order->customer->name);
});

it('gates the provisioning console to admins', function () {
    $this->get(route('admin.provisioning'))->assertRedirect(route('admin.login'));
    $this->actingAs(User::factory()->create())
        ->get(route('admin.provisioning'))->assertForbidden();
});

it('shows the customer their own live provisioning progress', function () {
    $user = User::factory()->create(['email' => 'k@example.test', 'is_admin' => false]);
    $customer = Customer::factory()->create(['email' => 'k@example.test']);
    $order = Order::factory()->create(['customer_id' => $customer->id]);
    ProvisioningRun::factory()->create([
        'subject_type' => Order::class, 'subject_id' => $order->id,
        'pipeline' => 'customer', 'status' => 'running', 'current_step' => 3,
    ]);

    Livewire::actingAs($user)->test(CustomerProvisioning::class)
        ->assertSee(__('dashboard.provisioning_title'));
});

it('renders nothing once the customer provisioning has completed', function () {
    $user = User::factory()->create(['email' => 'done@example.test']);
    $customer = Customer::factory()->create(['email' => 'done@example.test']);
    $order = Order::factory()->create(['customer_id' => $customer->id]);
    ProvisioningRun::factory()->create([
        'subject_type' => Order::class, 'subject_id' => $order->id,
        'pipeline' => 'customer', 'status' => 'completed', 'current_step' => 14,
    ]);

    Livewire::actingAs($user)->test(CustomerProvisioning::class)
        ->assertDontSee(__('dashboard.provisioning_title'));
});
