<?php

namespace Database\Seeders;

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

class ProductSeeder extends Seeder
{
    public function run(): void
    {
        // Create weights
        $weightLabels = ['10KG', '15KG', '20KG', '30KG', '35KG', '37KG', '40KG', '50KG', '10KG Super'];
        $weights = [];
        foreach ($weightLabels as $i => $label) {
            $weights[$label] = Weight::firstOrCreate(['label' => $label], ['sort_order' => ($i + 1) * 10]);
        }

        // Create categories and products
        $data = [
            'Flour' => [
                ['weight' => '10KG Super', 'price' => 650, 'stock' => 50],
                ['weight' => '15KG', 'price' => 950, 'stock' => 40],
                ['weight' => '20KG', 'price' => 1250, 'stock' => 30],
            ],
            'Chowker' => [
                ['weight' => '10KG', 'price' => 700, 'stock' => 25],
                ['weight' => '20KG', 'price' => 1350, 'stock' => 20],
            ],
            'Khal' => [
                ['weight' => '20KG', 'price' => 800, 'stock' => 15],
                ['weight' => '50KG', 'price' => 1900, 'stock' => 10],
            ],
            'Maida' => [
                ['weight' => '10KG', 'price' => 750, 'stock' => 20],
                ['weight' => '20KG', 'price' => 1450, 'stock' => 15],
            ],
            'Suji' => [
                ['weight' => '10KG', 'price' => 900, 'stock' => 10],
            ],
            'Besan' => [
                ['weight' => '10KG', 'price' => 1100, 'stock' => 10],
            ],
        ];

        foreach ($data as $catName => $products) {
            $category = Category::firstOrCreate(['name' => $catName]);
            foreach ($products as $p) {
                Product::create([
                    'category_id' => $category->id,
                    'weight_id' => $weights[$p['weight']]->id,
                    'name' => $catName,
                    'price' => $p['price'],
                    'stock' => $p['stock'],
                ]);
            }
        }
    }
}
