<?php

namespace Tests\Feature;

use App\Models\Branch;
use App\Models\Category;
use App\Models\Customer;
use App\Models\CustomerOrderRequest;
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 CustomerRequestWorkflowTest extends TestCase
{
    use RefreshDatabase;

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

        $pickup = CustomerOrderRequest::query()->create([
            'branch_id' => $customer->branch_id,
            'customer_id' => $customer->id,
            'type' => 'pickup',
            'status' => 'baru',
            'payload' => [
                'pickup_address' => 'Jl. Pickup',
                'pickup_date' => now()->addDay()->toDateString(),
                'pickup_time' => '10:00',
            ],
        ]);

        $this->actingAs($admin)
            ->get(route('customer-requests.index'))
            ->assertOk()
            ->assertSee('Preview Portal')
            ->assertSee('Jl. Pickup');

        $this->actingAs($admin)
            ->get(route('customer-requests.portal-preview', $pickup))
            ->assertOk()
            ->assertSee('Mode preview admin')
            ->assertSee($customer->name);

        $this->actingAs($admin)
            ->post(route('customer-requests.review', $pickup), ['note' => 'Sudah dihubungi'])
            ->assertRedirect();

        $this->assertSame('direview', $pickup->refresh()->status);

        $this->actingAs($admin)
            ->post(route('customer-requests.cancel', $pickup), ['note' => 'Pelanggan batal'])
            ->assertRedirect();

        $this->assertSame('dibatalkan', $pickup->refresh()->status);

        $repeat = CustomerOrderRequest::query()->create([
            'branch_id' => $customer->branch_id,
            'customer_id' => $customer->id,
            'source_order_id' => $sourceOrder->id,
            'type' => 'repeat',
            'status' => 'direview',
            'payload' => ['source_order_id' => $sourceOrder->id, 'note' => 'Ulangi nota ini'],
        ]);

        $ordersBefore = $customer->orders()->count();

        $this->actingAs($admin)
            ->post(route('customer-requests.convert', $repeat))
            ->assertRedirect();

        $this->assertSame('dikonversi', $repeat->refresh()->status);
        $this->assertSame($ordersBefore + 1, $customer->orders()->count());
        $this->assertSame(
            $sourceOrder->details()->first()->product_id,
            $customer->orders()->latest()->first()->details()->first()->product_id,
        );
    }

    private function fixtures(): array
    {
        $branch = Branch::query()->create(['name' => 'Cabang Request', 'code' => 'REQ']);
        $admin = User::query()->create([
            'branch_id' => $branch->id,
            'name' => 'Admin Cabang',
            'email' => 'admin-request@example.test',
            'role' => 'admin_cabang',
            'password' => Hash::make('password'),
        ]);
        $customer = Customer::query()->create([
            'branch_id' => $branch->id,
            'name' => 'Request Customer',
            'phone' => '62818880000',
            'address' => 'Jl. Customer',
            'pin' => Hash::make('000000'),
        ]);
        $category = Category::query()->create(['name' => 'Service', 'type' => 'service']);
        $service = Product::query()->create([
            'category_id' => $category->id,
            'name' => 'Cuci Regular',
            'sku' => 'SRV-REQ',
            'type' => 'service',
            'unit' => 'kg',
            'price' => 8000,
        ]);
        $sourceOrder = app(OrderService::class)->checkout([
            'branch_id' => $branch->id,
            'customer_id' => $customer->id,
            'items' => [['product_id' => $service->id, 'quantity' => 2, 'staff_id' => $admin->id]],
            'paid_amount' => 16000,
        ], $admin);

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