<?php

namespace Tests\Feature;

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

class DigitalFormEvidenceTest extends TestCase
{
    use RefreshDatabase;

    public function test_staff_can_store_note_level_evidence_and_signature(): void
    {
        Storage::fake('public');
        [$staff, $order] = $this->fixtures();
        $image = 'data:image/jpeg;base64,'.base64_encode('fake-image-content');
        $signature = 'data:image/png;base64,'.base64_encode('fake-signature-content');

        $this->actingAs($staff)
            ->post(route('digital-forms.store', $order), [
                'checklist' => [
                    'ada_noda' => '1',
                    'ada_robek' => '1',
                ],
                'general_note' => 'Ada noda di kerah dan customer menyetujui kondisi awal.',
                'evidence_images' => [$image],
                'signature_data' => $signature,
            ])
            ->assertRedirect();

        $form = DigitalForm::query()->firstOrFail();

        $this->assertSame($order->id, $form->order_id);
        $this->assertCount(1, $form->evidence_paths);
        $this->assertNotNull($form->signature_path);
        Storage::disk('public')->assertExists($form->evidence_paths[0]);
        Storage::disk('public')->assertExists($form->signature_path);

        $this->actingAs($staff)
            ->get(route('digital-forms.show', $form))
            ->assertOk()
            ->assertSee('Ada noda di kerah');
    }

    private function fixtures(): array
    {
        $branch = Branch::query()->create(['name' => 'Cabang Evidence', 'code' => 'EVD']);
        $staff = User::query()->create([
            'branch_id' => $branch->id,
            'name' => 'Staff Evidence',
            'email' => 'staff-evidence@example.test',
            'role' => 'kasir',
            'password' => Hash::make('password'),
        ]);
        $customer = Customer::query()->create([
            'branch_id' => $branch->id,
            'name' => 'Customer Evidence',
            'phone' => '62815550000',
            'pin' => Hash::make('000000'),
        ]);
        $category = Category::query()->create(['name' => 'Service', 'type' => 'service']);
        $service = Product::query()->create([
            'category_id' => $category->id,
            'name' => 'Cuci Evidence',
            'sku' => 'SRV-EVIDENCE',
            'type' => 'service',
            'unit' => 'kg',
            'price' => 10000,
        ]);
        $order = app(OrderService::class)->checkout([
            'branch_id' => $branch->id,
            'customer_id' => $customer->id,
            'items' => [['product_id' => $service->id, 'quantity' => 1, 'staff_id' => $staff->id]],
            'paid_amount' => 10000,
        ], $staff);

        return [$staff, $order];
    }
}
