<?php

namespace Tests\Feature;

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

class MobileIntakeTest extends TestCase
{
    use RefreshDatabase;

    public function test_staff_can_create_mobile_intake_order_and_continue_to_evidence_form(): void
    {
        [$staff, $service] = $this->fixtures();

        $this->actingAs($staff)
            ->get(route('mobile-intake.create'))
            ->assertOk()
            ->assertSee('Mobile Intake POS')
            ->assertSee('Cari produk / layanan')
            ->assertDontSee($service->name);

        $this->actingAs($staff)
            ->post(route('mobile-intake.store'), [
                'customer_mode' => 'new',
                'new_customer' => [
                    'name' => 'Pelanggan Mobile',
                    'phone' => '62818889999',
                    'address' => 'Jl. Mobile',
                ],
                'delivery_fee' => 3000,
                'paid_amount' => 5000,
                'payment_method' => 'cash',
                'note' => 'Diterima via HP',
                'items' => [
                    [
                        'product_id' => $service->id,
                        'quantity' => 2.5,
                        'discount' => 0,
                    ],
                ],
            ])
            ->assertRedirect();

        $customer = Customer::query()->where('phone', '62818889999')->firstOrFail();
        $order = Order::query()->where('customer_id', $customer->id)->firstOrFail();

        $this->assertSame('Diterima via HP', $order->note);
        $this->assertSame(28000.0, (float) $order->grand_total);
        $this->assertSame(23000.0, (float) $order->balance_due);
        $this->assertDatabaseHas('order_details', [
            'order_id' => $order->id,
            'product_id' => $service->id,
            'product_name' => $service->name,
        ]);
    }

    public function test_customer_search_finds_by_name_or_phone_and_is_branch_scoped(): void
    {
        [$staff] = $this->fixtures();
        Customer::query()->create([
            'branch_id' => $staff->branch_id,
            'name' => 'Siti Laundry',
            'phone' => '62819990000',
            'pin' => Hash::make('000000'),
        ]);
        Customer::query()->create([
            'branch_id' => Branch::query()->create(['name' => 'Cabang Lain', 'code' => 'OTH'])->id,
            'name' => 'Siti Cabang Lain',
            'phone' => '62819991111',
            'pin' => Hash::make('000000'),
        ]);

        $this->actingAs($staff)
            ->getJson(route('pos.customers.search', ['q' => 'Siti']))
            ->assertOk()
            ->assertJsonFragment(['name' => 'Siti Laundry'])
            ->assertJsonMissing(['name' => 'Siti Cabang Lain']);

        $this->actingAs($staff)
            ->getJson(route('pos.customers.search', ['q' => '9990000']))
            ->assertOk()
            ->assertJsonFragment(['phone' => '62819990000']);
    }

    public function test_product_search_finds_active_product_by_name_or_sku(): void
    {
        [$staff, $service] = $this->fixtures();
        Product::query()->create([
            'category_id' => $service->category_id,
            'name' => 'Produk Nonaktif',
            'sku' => 'NONACTIVE',
            'type' => 'service',
            'unit' => 'kg',
            'price' => 10000,
            'is_active' => false,
        ]);

        $this->actingAs($staff)
            ->getJson(route('pos.products.search', ['q' => 'Mobile']))
            ->assertOk()
            ->assertJsonFragment(['name' => 'Cuci Mobile'])
            ->assertJsonMissing(['name' => 'Produk Nonaktif']);

        $this->actingAs($staff)
            ->getJson(route('pos.products.search', ['q' => 'SRV-MOBILE']))
            ->assertOk()
            ->assertJsonFragment(['sku' => 'SRV-MOBILE']);
    }

    private function fixtures(): array
    {
        $branch = Branch::query()->create(['name' => 'Cabang Mobile', 'code' => 'MOB']);
        $staff = User::query()->create([
            'branch_id' => $branch->id,
            'name' => 'Staff Mobile',
            'email' => 'staff-mobile@example.test',
            'role' => 'kasir',
            'password' => Hash::make('password'),
        ]);
        $category = Category::query()->create(['name' => 'Service', 'type' => 'service']);
        $service = Product::query()->create([
            'category_id' => $category->id,
            'name' => 'Cuci Mobile',
            'sku' => 'SRV-MOBILE',
            'type' => 'service',
            'unit' => 'kg',
            'price' => 10000,
            'is_active' => true,
        ]);

        return [$staff, $service];
    }
}
