<?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 CustomerPortalFlowTest extends TestCase
{
    use RefreshDatabase;

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

        $this->post(route('portal.authenticate'), [
            'phone' => $customer->phone,
            'pin' => '000000',
        ])->assertRedirect(route('portal.dashboard'));

        $this->withSession(['customer_id' => $customer->id])
            ->get(route('portal.dashboard', ['invoice_no' => $order->invoice_no]))
            ->assertOk()
            ->assertSee($order->invoice_no);

        $this->withSession(['customer_id' => $customer->id])
            ->get(route('portal.orders.show', $order))
            ->assertOk()
            ->assertSee('Repeat Order dari Nota Ini');

        $this->withSession(['customer_id' => $customer->id])
            ->get(route('portal.requests'))
            ->assertOk()
            ->assertSee('Request Pickup')
            ->assertSee('Request Lainnya');

        $this->withSession(['customer_id' => $customer->id])
            ->post(route('portal.requests.store'), [
                'type' => 'pickup',
                'pickup_address' => 'Jl. Mawar No. 1',
                'pickup_date' => now()->addDay()->toDateString(),
                'pickup_time' => '09:00',
                'note' => 'Ambil pagi',
            ])->assertRedirect();

        $this->assertDatabaseHas('customer_order_requests', [
            'customer_id' => $customer->id,
            'type' => 'pickup',
            'status' => 'baru',
        ]);

        $this->withSession(['customer_id' => $customer->id])
            ->post(route('portal.requests.store'), [
                'type' => 'repeat',
                'source_order_id' => $order->id,
                'note' => 'Sama seperti nota ini',
            ])->assertRedirect();

        $this->assertDatabaseHas('customer_order_requests', [
            'customer_id' => $customer->id,
            'type' => 'repeat',
            'source_order_id' => $order->id,
        ]);

        $this->withSession(['customer_id' => $customer->id])
            ->post(route('portal.requests.store'), [
                'type' => 'general',
                'note' => 'Tanya jam operasional pickup',
            ])->assertRedirect();

        $this->assertDatabaseHas('customer_order_requests', [
            'customer_id' => $customer->id,
            'type' => 'general',
            'note' => 'Tanya jam operasional pickup',
        ]);

        $this->withSession(['customer_id' => $customer->id])
            ->get(route('portal.account'))
            ->assertOk()
            ->assertSee('Akun pelanggan');

        $this->withSession(['customer_id' => $customer->id])
            ->put(route('portal.account.update'), [
                'name' => 'Portal Customer Update',
                'address' => 'Jl. Portal Baru',
            ])->assertRedirect();

        $this->assertDatabaseHas('customers', [
            'id' => $customer->id,
            'name' => 'Portal Customer Update',
            'address' => 'Jl. Portal Baru',
        ]);

        $this->withSession(['customer_id' => $customer->id])
            ->post(route('portal.pin.update'), [
                'current_pin' => '000000',
                'pin' => '123456',
                'pin_confirmation' => '123456',
            ])->assertRedirect();

        $this->assertTrue(Hash::check('123456', $customer->refresh()->pin));
    }

    private function fixtures(): array
    {
        $branch = Branch::query()->create(['name' => 'Cabang Portal', 'code' => 'PRT']);
        $cashier = User::query()->create([
            'branch_id' => $branch->id,
            'name' => 'Kasir',
            'email' => 'portal-kasir@example.test',
            'role' => 'kasir',
            'password' => Hash::make('password'),
        ]);
        $customer = Customer::query()->create([
            'branch_id' => $branch->id,
            'name' => 'Portal Customer',
            'phone' => '62817770000',
            'address' => 'Jl. Portal',
            'pin' => Hash::make('000000'),
        ]);
        $category = Category::query()->create(['name' => 'Service', 'type' => 'service']);
        $service = Product::query()->create([
            'category_id' => $category->id,
            'name' => 'Cuci Setrika',
            'sku' => 'SRV-PORTAL',
            '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]],
            'paid_amount' => 9000,
        ], $cashier);

        return [$customer, $order];
    }
}
