<?php

namespace Database\Seeders;

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

class InventorySeeder extends Seeder
{
    public function run(): void
    {
        // Bersihkan data ukuran di database terpisah agar sinkron dengan produk baru
        try {
            \Illuminate\Support\Facades\DB::connection('sizes_db')->table('product_sizes')->delete();
        } catch (\Exception $e) {
            // abaikan jika tabel belum ada
        }

        $atasan = Category::create(['name' => 'Atasan']);
        $bawahan = Category::create(['name' => 'Bawahan']);
        $outer = Category::create(['name' => 'Outer / Jaket']);
        $dress = Category::create(['name' => 'Dress']);
        $sport = Category::create(['name' => 'Pakaian Olahraga']);
        $piyama = Category::create(['name' => 'Piyama & Loungewear']);

        $products = [
            ['category_id' => $atasan->id, 'name' => 'Kaos Polos Cotton', 'image_url' => 'https://via.placeholder.com/320x240?text=Kaos+Polos', 'size' => 'M', 'color' => 'Hitam', 'stock' => 25, 'min_stock' => 10, 'max_stock' => 50],
            ['category_id' => $atasan->id, 'name' => 'Kemeja Kerja Putih', 'image_url' => 'https://via.placeholder.com/320x240?text=Kemeja+Putih', 'size' => 'L', 'color' => 'Putih', 'stock' => 18, 'min_stock' => 10, 'max_stock' => 40],
            ['category_id' => $atasan->id, 'name' => 'Blouse Wanita Floral', 'image_url' => 'https://via.placeholder.com/320x240?text=Blouse+Floral', 'size' => 'S', 'color' => 'Merah+Muda', 'stock' => 12, 'min_stock' => 10, 'max_stock' => 35],
            ['category_id' => $atasan->id, 'name' => 'Tunik Batik Modern', 'image_url' => 'https://via.placeholder.com/320x240?text=Tunik+Batik', 'size' => 'M', 'color' => 'Navy', 'stock' => 10, 'min_stock' => 10, 'max_stock' => 30],
            ['category_id' => $atasan->id, 'name' => 'Tank Top Sporty', 'image_url' => 'https://via.placeholder.com/320x240?text=Tank+Top', 'size' => 'L', 'color' => 'Abu-abu', 'stock' => 22, 'min_stock' => 10, 'max_stock' => 45],

            ['category_id' => $bawahan->id, 'name' => 'Celana Jeans Slim-fit', 'image_url' => 'https://via.placeholder.com/320x240?text=Jeans+Slim', 'size' => '30', 'color' => 'Biru', 'stock' => 24, 'min_stock' => 10, 'max_stock' => 50],
            ['category_id' => $bawahan->id, 'name' => 'Rok Midi Polos', 'image_url' => 'https://via.placeholder.com/320x240?text=Rok+Midi', 'size' => 'M', 'color' => 'Cokelat', 'stock' => 16, 'min_stock' => 10, 'max_stock' => 40],
            ['category_id' => $bawahan->id, 'name' => 'Celana Chino Pria', 'image_url' => 'https://via.placeholder.com/320x240?text=Chino+Pria', 'size' => '32', 'color' => 'Krem', 'stock' => 14, 'min_stock' => 10, 'max_stock' => 35],
            ['category_id' => $bawahan->id, 'name' => 'Legging Olahraga', 'image_url' => 'https://via.placeholder.com/320x240?text=Legging', 'size' => 'S', 'color' => 'Hitam', 'stock' => 33, 'min_stock' => 10, 'max_stock' => 50],
            ['category_id' => $bawahan->id, 'name' => 'Shorts Denim', 'image_url' => 'https://via.placeholder.com/320x240?text=Shorts+Denim', 'size' => 'M', 'color' => 'Biru+Muda', 'stock' => 20, 'min_stock' => 10, 'max_stock' => 40],

            ['category_id' => $outer->id, 'name' => 'Jaket Bomber Pilot', 'image_url' => 'https://via.placeholder.com/320x240?text=Jaket+Bomber', 'size' => 'XL', 'color' => 'Hijau+Army', 'stock' => 4, 'min_stock' => 10, 'max_stock' => 30],
            ['category_id' => $outer->id, 'name' => 'Cardigan Premium Rajut', 'image_url' => 'https://via.placeholder.com/320x240?text=Cardigan', 'size' => 'S', 'color' => 'Maroon', 'stock' => 45, 'min_stock' => 10, 'max_stock' => 40],
            ['category_id' => $outer->id, 'name' => 'Hoodie Cotton Fleece', 'image_url' => 'https://via.placeholder.com/320x240?text=Hoodie', 'size' => 'L', 'color' => 'Abu+Tua', 'stock' => 28, 'min_stock' => 10, 'max_stock' => 50],
            ['category_id' => $outer->id, 'name' => 'Windbreaker Water Resistant', 'image_url' => 'https://via.placeholder.com/320x240?text=Windbreaker', 'size' => 'M', 'color' => 'Biru+Gelap', 'stock' => 9, 'min_stock' => 10, 'max_stock' => 30],
            ['category_id' => $outer->id, 'name' => 'Parka Winter', 'image_url' => 'https://via.placeholder.com/320x240?text=Parka+Winter', 'size' => 'XL', 'color' => 'Hitam', 'stock' => 6, 'min_stock' => 10, 'max_stock' => 25],

            ['category_id' => $dress->id, 'name' => 'Dress Pesta Satin', 'image_url' => 'https://via.placeholder.com/320x240?text=Dress+Satin', 'size' => 'M', 'color' => 'Marun', 'stock' => 7, 'min_stock' => 10, 'max_stock' => 25],
            ['category_id' => $dress->id, 'name' => 'Mini Dress Polos', 'image_url' => 'https://via.placeholder.com/320x240?text=Mini+Dress', 'size' => 'S', 'color' => 'Krem', 'stock' => 13, 'min_stock' => 10, 'max_stock' => 35],
            ['category_id' => $dress->id, 'name' => 'Maxi Dress Floral', 'image_url' => 'https://via.placeholder.com/320x240?text=Maxi+Dress', 'size' => 'L', 'color' => 'Hijau', 'stock' => 11, 'min_stock' => 10, 'max_stock' => 30],
            ['category_id' => $dress->id, 'name' => 'Dress Midi Batik', 'image_url' => 'https://via.placeholder.com/320x240?text=Dress+Batik', 'size' => 'M', 'color' => 'Cokelat', 'stock' => 8, 'min_stock' => 10, 'max_stock' => 25],

            ['category_id' => $sport->id, 'name' => 'Set Olahraga Training', 'image_url' => 'https://via.placeholder.com/320x240?text=Set+Training', 'size' => 'M', 'color' => 'Hitam', 'stock' => 19, 'min_stock' => 10, 'max_stock' => 40],
            ['category_id' => $sport->id, 'name' => 'Polo Shirt Golf', 'image_url' => 'https://via.placeholder.com/320x240?text=Polo+Golf', 'size' => 'L', 'color' => 'Putih', 'stock' => 21, 'min_stock' => 10, 'max_stock' => 45],
            ['category_id' => $sport->id, 'name' => 'Shorts Basketball', 'image_url' => 'https://via.placeholder.com/320x240?text=Shorts+Basketball', 'size' => 'XL', 'color' => 'Oranye', 'stock' => 12, 'min_stock' => 10, 'max_stock' => 35],

            ['category_id' => $piyama->id, 'name' => 'Set Piyama Katun', 'image_url' => 'https://via.placeholder.com/320x240?text=Piyama+Katun', 'size' => 'M', 'color' => 'Biru+Muda', 'stock' => 17, 'min_stock' => 10, 'max_stock' => 40],
            ['category_id' => $piyama->id, 'name' => 'Loungewear Hoodie', 'image_url' => 'https://via.placeholder.com/320x240?text=Loungewear+Hoodie', 'size' => 'L', 'color' => 'Abu-abu', 'stock' => 20, 'min_stock' => 10, 'max_stock' => 40],
            ['category_id' => $piyama->id, 'name' => 'Set Kemeja Tidur', 'image_url' => 'https://via.placeholder.com/320x240?text=Kemeja+Tidur', 'size' => 'S', 'color' => 'Putih', 'stock' => 9, 'min_stock' => 10, 'max_stock' => 30],
        ];

        foreach ($products as $product) {
            $initialStock = $product['stock'] ?? 0;
            $minStock = $product['min_stock'] ?? 10;
            $maxStock = $product['max_stock'] ?? 50;

            $productData = [
                'category_id' => $product['category_id'],
                'name' => $product['name'],
                'image_url' => $product['image_url'],
                'color' => $product['color'],
                'size' => '',
                'stock' => 0,
                'min_stock' => $minStock,
                'max_stock' => $maxStock,
            ];

            $p = Product::create($productData);

            $standardSizes = ['S', 'M', 'L', 'XL', 'XXL'];
            $sizeCount = count($standardSizes);
            $stockPerSize = intdiv($initialStock, $sizeCount);
            $remainder = $initialStock % $sizeCount;

            foreach ($standardSizes as $index => $size) {
                $stock = $stockPerSize + ($index < $remainder ? 1 : 0);
                ProductSize::create([
                    'product_id' => $p->id,
                    'size' => $size,
                    'stock' => $stock,
                ]);
            }

            $p->syncStock();

            if ($p->total_stock > 0) {
                $p->logStockMovement('in', $p->total_stock, 0, $p->total_stock, 'Stok awal dari seeder');
            }
        }
    }
}
