<?php

namespace Database\Seeders;

use App\Modules\Inventory\Models\Category;
use App\Modules\Inventory\Models\Product;
use Illuminate\Database\Seeder;

class ProductSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 取得分類 ID
        $rawMaterialId = Category::where('name', '原物料')->first()->id;
        $packagingId = Category::where('name', '包材')->first()->id;

        $products = [
            // 原物料
            [
                'code' => 'RM001',
                'name' => '二砂糖',
                'category_id' => $rawMaterialId,
                'brand' => '台糖',
                'specification' => '業務用 50kg/袋',
                'base_unit' => 'g',
                'large_unit' => '袋',
                'conversion_rate' => 50000,
                'purchase_unit' => '袋',
            ],
            [
                'code' => 'RM002',
                'name' => '錫蘭紅茶',
                'category_id' => $rawMaterialId,
                'brand' => '天仁',
                'specification' => '特級茶葉',
                'base_unit' => 'g',
                'large_unit' => '箱',
                'conversion_rate' => 30000, // 假設一箱 30kg
                'purchase_unit' => '箱',
            ],
            [
                'code' => 'RM003',
                'name' => '鮮乳',
                'category_id' => $rawMaterialId,
                'brand' => '光泉',
                'specification' => '業務用鮮乳 1公升',
                'base_unit' => 'ml',
                'large_unit' => '瓶',
                'conversion_rate' => 960, // 960ml
                'purchase_unit' => '瓶',
            ],
             // 包材
            [
                'code' => 'PC001',
                'name' => 'PP飲料杯 700cc',
                'category_id' => $packagingId,
                'brand' => '南亞',
                'specification' => '透明, 95口徑',
                'base_unit' => '個',
                'large_unit' => '箱',
                'conversion_rate' => 1000, // 1箱1000個
                'purchase_unit' => '箱',
            ],
             [
                'code' => 'PC002',
                'name' => '粗吸管',
                'category_id' => $packagingId,
                'brand' => '',
                'specification' => '獨立包裝, 12mm',
                'base_unit' => '支',
                'large_unit' => '包',
                'conversion_rate' => 100, // 1包100支
                'purchase_unit' => '箱', // 假設採購單位是箱，這裡可能需要邏輯調整，但先跟著轉換率
            ],
        ];

        foreach ($products as $product) {
            // 使用 firstOrCreate 避免重複建立
            Product::firstOrCreate(['code' => $product['code']], $product);
        }

        // 額外隨機建立 25 筆商品以測試分頁
        Product::factory()->count(25)->create();
    }
}
