<?php

namespace Database\Seeders;

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

class ProductSeeder extends Seeder
{
    public function run(): void
    {
        $categories = Category::all();

        $productsByCategory = [
            'Elektronik' => [
                [
                    'name' => 'Smartphone Samsung Galaxy A54',
                    'sku' => 'ELEC001',
                    'price' => 4500000,
                    'stock' => 25,
                    'bulk_unit' => 'Karton',
                    'bulk_quantity' => 1,
                    'retail_unit' => 'pcs',
                    'image' => 'https://picsum.photos/id/0/200/200',
                    'description' => 'Smartphone terbaru dengan layar Super AMOLED 6.4 inch.'
                ],
                // ... produk lainnya
            ],
            'Makanan' => [
                [
                    'name' => 'Indomie Goreng',
                    'sku' => 'FOOD001',
                    'price' => 42000, // Harga 1 karton
                    'stock' => 100,
                    'bulk_unit' => 'Karton',
                    'bulk_quantity' => 40, // 1 karton isi 40 pcs
                    'retail_unit' => 'pcs',
                    'image' => 'https://picsum.photos/id/10/200/200',
                    'description' => 'Indomie goreng original. 1 karton isi 40 pcs.'
                ],
                [
                    'name' => 'Beras Premium',
                    'sku' => 'FOOD003',
                    'price' => 250000, // Harga 1 karung
                    'stock' => 50,
                    'bulk_unit' => 'Karung',
                    'bulk_quantity' => 20, // 1 karung isi 20 kg
                    'retail_unit' => 'kg',
                    'image' => 'https://picsum.photos/id/11/200/200',
                    'description' => 'Beras premium kualitas terbaik. 1 karung isi 20 kg.'
                ],
                [
                    'name' => 'Minyak Goreng',
                    'sku' => 'FOOD005',
                    'price' => 180000, // Harga 1 karton
                    'stock' => 40,
                    'bulk_unit' => 'Karton',
                    'bulk_quantity' => 12, // 1 karton isi 12 L
                    'retail_unit' => 'L',
                    'image' => 'https://picsum.photos/id/13/200/200',
                    'description' => 'Minyak goreng sehat. 1 karton isi 12 L.'
                ],
                [
                    'name' => 'Gula Pasir',
                    'sku' => 'FOOD004',
                    'price' => 150000, // Harga 1 karung
                    'stock' => 200,
                    'bulk_unit' => 'Karung',
                    'bulk_quantity' => 10, // 1 karung isi 10 kg
                    'retail_unit' => 'kg',
                    'image' => 'https://picsum.photos/id/12/200/200',
                    'description' => 'Gula pasir putih berkualitas. 1 karung isi 10 kg.'
                ],
            ],
            'Minuman' => [
                [
                    'name' => 'Teh Botol Sosro',
                    'sku' => 'DRNK001',
                    'price' => 60000, // Harga 1 karton
                    'stock' => 80,
                    'bulk_unit' => 'Karton',
                    'bulk_quantity' => 12, // 1 karton isi 12 botol
                    'retail_unit' => 'botol',
                    'image' => 'https://picsum.photos/id/15/200/200',
                    'description' => 'Teh botol segar. 1 karton isi 12 botol.'
                ],
                [
                    'name' => 'Aqua 600ml',
                    'sku' => 'DRNK002',
                    'price' => 36000, // Harga 1 karton
                    'stock' => 100,
                    'bulk_unit' => 'Karton',
                    'bulk_quantity' => 12, // 1 karton isi 12 botol
                    'retail_unit' => 'botol',
                    'image' => 'https://picsum.photos/id/16/200/200',
                    'description' => 'Air mineral murni. 1 karton isi 12 botol.'
                ],
            ],
        ];

        $this->command->info('Memproses produk...');

        $totalProducts = 0;
        $imageCounter = 10;

        foreach ($categories as $category) {
            $categoryName = $category->name;

            if (isset($productsByCategory[$categoryName])) {
                $products = $productsByCategory[$categoryName];
                $count = 0;

                foreach ($products as $index => $productData) {
                    $imageUrl = $productData['image'] ?? "https://picsum.photos/id/{$imageCounter}/200/200";
                    $description = $productData['description'] ?? "Produk {$productData['name']} berkualitas tinggi.";

                    Product::updateOrCreate(
                        ['sku' => $productData['sku']],
                        [
                            'category_id' => $category->id,
                            'name' => $productData['name'],
                            'price' => $productData['price'],
                            'stock' => $productData['stock'],
                            'bulk_unit' => $productData['bulk_unit'] ?? 'Karton',
                            'bulk_quantity' => $productData['bulk_quantity'] ?? 1,
                            'retail_unit' => $productData['retail_unit'] ?? 'pcs',
                            'image' => $imageUrl,
                            'description' => $description,
                        ]
                    );
                    $count++;
                    $totalProducts++;
                    $imageCounter++;
                }

                $this->command->info("✓ Kategori '{$categoryName}': {$count} produk");
            }
        }

        $this->command->newLine();
        $this->command->info('═══════════════════════════════════');
        $this->command->info('✅ PRODUK BERHASIL DITAMBAHKAN!');
        $this->command->info('═══════════════════════════════════');
        $this->command->info("Total produk: {$totalProducts} produk");
        $this->command->info('═══════════════════════════════════');
    }
}
