<?php

namespace Database\Seeders;

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

class IceCreamContainerProductsSeeder extends Seeder
{
    /**
     * Seed ice cream container products (Small Cup, Medium Cup, Family Deal)
     */
    public function run(): void
    {
        $this->command->info('');
        $this->command->info('🍦 Ice Cream Container Products Seeder');
        $this->command->info('═══════════════════════════════════════');
        $this->command->info('');

        DB::transaction(function () {
            // Find or create Ice Cream category
            $category = Category::firstOrCreate(
                ['name' => 'Ice Cream Containers'],
                [
                    'slug' => 'ice-cream-containers',
                    'description' => 'Pre-portioned ice cream serving sizes',
                    'is_active' => true,
                ]
            );

            $this->command->info("📁 Category: {$category->name}");
            $this->command->info('');

            // Container products configuration
            $containers = [
                [
                    'name' => 'Small Cup Ice Cream',
                    'description' => 'Perfect for a quick treat - Choose any 2 scoops',
                    'category_id' => $category->id,
                    'sku' => 'ICE-CUP-SMALL-2',
                    'price' => 7.50,
                    'base_price' => 3.50,
                    'price_per_scoop' => 2.00,
                    'scoop_capacity' => 2,
                    'unit_of_measure' => 'cup',
                    'is_active' => true,
                    'has_inner_options' => true, // Allows flavour selection
                    'metadata' => json_encode([
                        'container_type' => 'small_cup',
                        'total_weight_grams' => 120,
                        'weight_per_scoop_grams' => 60,
                        'serving_size' => '2 scoops',
                        'recommended_for' => 'Individual serving',
                    ]),
                ],
                [
                    'name' => 'Medium Cup Ice Cream',
                    'description' => 'Most popular choice - Choose any 3 scoops',
                    'category_id' => $category->id,
                    'sku' => 'ICE-CUP-MEDIUM-3',
                    'price' => 9.50,
                    'base_price' => 3.50,
                    'price_per_scoop' => 2.00,
                    'scoop_capacity' => 3,
                    'unit_of_measure' => 'cup',
                    'is_active' => true,
                    'has_inner_options' => true,
                    'metadata' => json_encode([
                        'container_type' => 'medium_cup',
                        'total_weight_grams' => 170,
                        'weight_per_scoop_grams' => 56.67,
                        'serving_size' => '3 scoops',
                        'recommended_for' => 'Regular serving',
                        'is_popular' => true,
                    ]),
                ],
                [
                    'name' => 'Family Deal Ice Cream',
                    'description' => 'Best value! - Choose any 6 scoops, mix and match flavours',
                    'category_id' => $category->id,
                    'sku' => 'ICE-DEAL-FAMILY-6',
                    'price' => 18.00,
                    'base_price' => 6.00,
                    'price_per_scoop' => 2.00,
                    'scoop_capacity' => 6,
                    'unit_of_measure' => 'container',
                    'is_active' => true,
                    'has_inner_options' => true,
                    'metadata' => json_encode([
                        'container_type' => 'family_deal',
                        'total_weight_grams' => 310,
                        'weight_per_scoop_grams' => 51.67,
                        'serving_size' => '6 scoops',
                        'recommended_for' => 'Family sharing',
                        'is_deal' => true,
                        'savings_message' => 'Save $3.00 compared to individual pricing!',
                    ]),
                ],
            ];

            $this->command->info('🍨 Creating container products...');
            $this->command->info('');

            $createdProducts = [];

            foreach ($containers as $containerData) {
                $product = Product::updateOrCreate(
                    ['name' => $containerData['name']],
                    $containerData
                );

                $createdProducts[] = $product;

                $priceInfo = sprintf(
                    'Price: $%.2f (Base: $%.2f + %d scoops × $%.2f)',
                    $product->price,
                    $product->base_price,
                    $product->scoop_capacity,
                    $product->price_per_scoop
                );

                $metadata = json_decode($product->metadata, true);
                $weightInfo = sprintf(
                    'Weight: %dg total (%.2fg per scoop)',
                    $metadata['total_weight_grams'],
                    $metadata['weight_per_scoop_grams']
                );

                $sku = $metadata['sku'] ?? 'N/A';

                $this->command->info("✓ {$product->name}");
                $this->command->info("  SKU: {$sku}");
                $this->command->info("  {$priceInfo}");
                $this->command->info("  {$weightInfo}");
                $this->command->info("  Capacity: {$product->scoop_capacity} scoops");
                $this->command->info('');
            }

            // Create summary
            $this->command->info('═══════════════════════════════════════');
            $this->command->info('✅ Container Products Summary');
            $this->command->info('═══════════════════════════════════════');
            $this->command->info('');

            $totalProducts = count($createdProducts);
            $this->command->info("Total container products: {$totalProducts}");
            $this->command->info('');

            // Pricing comparison table
            $this->command->info('💰 Pricing Structure:');
            $this->command->info('');
            $this->command->table(
                ['Size', 'Scoops', 'Price', 'Per Scoop', 'Weight', 'Best For'],
                [
                    [
                        'Small Cup',
                        '2',
                        '$7.50',
                        '$3.75',
                        '120g',
                        'Quick treat'
                    ],
                    [
                        'Medium Cup',
                        '3',
                        '$9.50',
                        '$3.17',
                        '170g',
                        'Regular serving ⭐'
                    ],
                    [
                        'Family Deal',
                        '6',
                        '$18.00',
                        '$3.00',
                        '310g',
                        'Best value! 💰'
                    ],
                ]
            );

            $this->command->info('');

            // Weight efficiency
            $this->command->info('⚖️  Weight Efficiency (per scoop):');
            $this->command->info('  • Small Cup: 60.00g/scoop (baseline)');
            $this->command->info('  • Medium Cup: 56.67g/scoop (5.6% smaller)');
            $this->command->info('  • Family Deal: 51.67g/scoop (13.9% smaller)');
            $this->command->info('');

            // Value proposition
            $this->command->info('💡 Value Proposition:');
            $this->command->info('  • Small Cup: $3.75 per scoop');
            $this->command->info('  • Medium Cup: $3.17 per scoop (15% savings)');
            $this->command->info('  • Family Deal: $3.00 per scoop (20% savings)');
            $this->command->info('');

            $this->command->info('═══════════════════════════════════════');
        });

        $this->command->info('');
        $this->command->info('✅ Container products seeded successfully!');
        $this->command->info('');
        $this->command->info('📝 Next Steps:');
        $this->command->info('  1. Run: php artisan db:seed --class=IceCreamRecipesTestSeeder');
        $this->command->info('  2. Test container products in POS');
        $this->command->info('  3. Verify weight calculations');
        $this->command->info('');
    }
}
