<?php

namespace Tests\Feature;

use App\Models\Branch;
use App\Models\Category;
use App\Models\Customer;
use App\Models\Product;
use App\Models\User;
use App\Services\OrderService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;

class PortalPreviewTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_can_preview_customer_portal_from_main_menu_without_customer_session(): void
    {
        [$admin, $customer, $order] = $this->fixtures();

        $this->actingAs($admin)
            ->get(route('portal-preview.index'))
            ->assertOk()
            ->assertSee('Preview Portal Pelanggan')
            ->assertSee($customer->name);

        $this->actingAs($admin)
            ->get(route('portal-preview.customer', $customer))
            ->assertOk()
            ->assertSee('Preview portal dari menu admin')
            ->assertSee(route('portal-preview.requests', $customer), false)
            ->assertSee(route('portal-preview.account', $customer), false)
            ->assertSee($order->invoice_no);

        $this->actingAs($admin)
            ->get(route('portal-preview.requests', $customer))
            ->assertOk()
            ->assertSee('Mode preview admin')
            ->assertSee('Riwayat Request')
            ->assertDontSee('Kirim Request Pickup');

        $this->actingAs($admin)
            ->get(route('portal-preview.account', $customer))
            ->assertOk()
            ->assertSee('Akun pelanggan')
            ->assertSee('Profil ditampilkan read-only')
            ->assertDontSee('Simpan PIN');

        $this->actingAs($admin)
            ->get(route('portal-preview.order', [$customer, $order]))
            ->assertOk()
            ->assertSee('Mode preview admin')
            ->assertSee($order->invoice_no);
    }

    private function fixtures(): array
    {
        $branch = Branch::query()->create(['name' => 'Cabang Preview', 'code' => 'PVW']);
        $admin = User::query()->create([
            'branch_id' => $branch->id,
            'name' => 'Admin Preview',
            'email' => 'admin-preview@example.test',
            'role' => 'admin_cabang',
            'password' => Hash::make('password'),
        ]);
        $customer = Customer::query()->create([
            'branch_id' => $branch->id,
            'name' => 'Preview Customer',
            'phone' => '62816660000',
            'pin' => Hash::make('000000'),
        ]);
        $category = Category::query()->create(['name' => 'Service', 'type' => 'service']);
        $service = Product::query()->create([
            'category_id' => $category->id,
            'name' => 'Cuci Preview',
            'sku' => 'SRV-PREVIEW',
            'type' => 'service',
            'unit' => 'kg',
            'price' => 9000,
        ]);
        $order = app(OrderService::class)->checkout([
            'branch_id' => $branch->id,
            'customer_id' => $customer->id,
            'items' => [['product_id' => $service->id, 'quantity' => 1, 'staff_id' => $admin->id]],
            'paid_amount' => 9000,
        ], $admin);

        return [$admin, $customer, $order];
    }
}
