<?php

namespace Tests\Feature;

use App\Modules\Core\Models\User;
use App\Modules\Inventory\Models\InventoryTransferOrder;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Imports\InventoryTransferItemImport;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Maatwebsite\Excel\Facades\Excel;
use Tests\TestCase;

class InventoryTransferImportTest extends TestCase
{
    use RefreshDatabase;

    protected $user;
    protected $fromWarehouse;
    protected $toWarehouse;
    protected $order;
    protected $product;

    protected function setUp(): void
    {
        parent::setUp();
        $this->user = User::create([
            'name' => 'Test User',
            'username' => 'testuser',
            'email' => 'test@example.com',
            'password' => bcrypt('password'),
        ]);
        $this->actingAs($this->user);

        $this->fromWarehouse = Warehouse::create([
            'code' => 'W1',
            'name' => 'From Warehouse',
            'type' => 'standard',
        ]);
        $this->toWarehouse = Warehouse::create([
            'code' => 'W2',
            'name' => 'To Warehouse',
            'type' => 'standard',
        ]);

        $this->order = InventoryTransferOrder::create([
            'doc_no' => 'TO' . time(),
            'from_warehouse_id' => $this->fromWarehouse->id,
            'to_warehouse_id' => $this->toWarehouse->id,
            'status' => 'draft',
            'created_by' => $this->user->id,
        ]);

        $this->product = Product::create([
            'code' => 'P001',
            'name' => 'Test Product',
            'status' => 'enabled',
        ]);
    }

    /** @test */
    public function it_can_import_items_with_chinese_headers()
    {
        // 建立假 Excel，使用中文標題
        $content = [
            ['商品代碼', '批號', '數量', '備註'],
            ['P001', 'BATCH001', '10', 'Imported Via Test'],
            ['P001', '', '5', 'Batch should be NO-BATCH'],
        ];

        // 這裡我們直接呼叫 Import 類別來測試，避免多層模擬
        $import = new InventoryTransferItemImport($this->order);
        
        // 我們模擬 Maatwebsite\Excel 傳入的 Collection
        // 注意：Excel 預設會將標題 slugify。如果 "商品代碼" 被 slugify，我們的 Import 類別會在那邊掛掉。
        // 所以這個測試可以幫我們確認 keys 是否如預期。
        
        // 如果 WithHeadingRow 是用 slug 處理，那 keys 會是 slug 化的版本。
        // 但如果我們在 Import 類別中直接讀取 $row['商品代碼']，我們得確定它真的在那裡。
        
        $rows = collect([
            collect(['商品代碼' => 'P001', '批號' => 'BATCH001', '數量' => '10', '備註' => 'Imported Via Test']),
            collect(['商品代碼' => 'P001', '批號' => '', '數量' => '5', '備註' => 'Batch should be NO-BATCH']),
        ]);

        $import->collection($rows);

        $this->assertDatabaseHas('inventory_transfer_items', [
            'transfer_order_id' => $this->order->id,
            'product_id' => $this->product->id,
            'batch_number' => 'BATCH001',
            'quantity' => 10,
        ]);

        $this->assertDatabaseHas('inventory_transfer_items', [
            'transfer_order_id' => $this->order->id,
            'product_id' => $this->product->id,
            'batch_number' => 'NO-BATCH',
            'quantity' => 5,
        ]);
    }
}
