<?php

namespace Database\Seeders;

use App\Models\Category;
use App\Models\Product;
use App\Models\ProductVariant;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class ProductSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $categories = Category::query()->pluck('id', 'slug');

        $products = [
            [
                'name' => 'Daster Batik Parang',
                'description' => 'Daster batik motif parang dengan bahan katun nyaman untuk pemakaian sehari-hari.',
                'category_slugs' => ['daster'],
                'variants' => [
                    ['name' => 'S', 'stock' => 25],
                    ['name' => 'M', 'stock' => 40],
                    ['name' => 'L', 'stock' => 35],
                    ['name' => 'XL', 'stock' => 20],
                ],
            ],
            [
                'name' => 'Setelan Celana Batik Modern',
                'description' => 'Setelan atasan dan celana batik dengan potongan modern untuk acara formal maupun kasual.',
                'category_slugs' => ['setelan-celana'],
                'variants' => [
                    ['name' => 'S', 'stock' => 15],
                    ['name' => 'M', 'stock' => 20],
                    ['name' => 'L', 'stock' => 18],
                    ['name' => 'XL', 'stock' => 12],
                ],
            ],
            [
                'name' => 'Blouse Batik Lengan Panjang',
                'description' => 'Blouse batik lengan panjang dengan detail kerah mandarin.',
                'category_slugs' => ['atasan'],
                'variants' => [
                    ['name' => 'S', 'stock' => 30],
                    ['name' => 'M', 'stock' => 35],
                    ['name' => 'L', 'stock' => 28],
                ],
            ],
            [
                'name' => 'Rok Lilit Batik',
                'description' => 'Rok lilit batik dengan motif klasik, mudah disesuaikan dengan berbagai ukuran.',
                'category_slugs' => ['bawahan'],
                'variants' => [
                    ['name' => 'All Size', 'stock' => 50],
                ],
            ],
            [
                'name' => 'Daster Busui Nursing',
                'description' => 'Daster busui dengan akses bukaan menyusui praktis dan bahan adem.',
                'category_slugs' => ['busui', 'daster'],
                'variants' => [
                    ['name' => 'M', 'stock' => 22],
                    ['name' => 'L', 'stock' => 26],
                    ['name' => 'XL', 'stock' => 18],
                ],
            ],
        ];

        try {
            DB::transaction(function () use ($products, $categories): void {
                foreach ($products as $productData) {
                    $product = Product::factory()->create([
                        'name' => $productData['name'],
                        'slug' => str()->slug($productData['name']),
                        'description' => $productData['description'],
                        'is_active' => true,
                    ]);

                    $product->categories()->sync(
                        collect($productData['category_slugs'])
                            ->map(fn (string $slug) => $categories[$slug])
                            ->all()
                    );

                    foreach ($productData['variants'] as $variantData) {
                        ProductVariant::factory()->create([
                            'product_id' => $product->id,
                            'name' => $variantData['name'],
                            'stock' => $variantData['stock'],
                        ]);
                    }
                }
            });
        } catch (\Throwable $e) {
            throw $e;
        }
    }
}
