<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\BusinessSetting;
use App\Models\ProductSheet;
use App\Models\ProductSheetItem;
use App\Models\ProductVariant;
use App\Models\ServiceSheet;
use App\Models\ServiceSheetItem;

class BusinessExampleSeeder extends Seeder
{
    /**
     * Seed business examples with 3 different types:
     * 1. Products only (E-commerce)
     * 2. Services only (Consultoria)
     * 3. Both (Gráfica + Design)
     */
    public function run(): void
    {
        $userId = 3; // Marco Leite

        // =====================================================================
        // NEGÓCIO 1: E-COMMERCE DE ELETRÔNICOS (Apenas Produtos)
        // =====================================================================
        $ecommerce = BusinessSetting::create([
            'user_id' => $userId,
            'name' => 'TechStore - E-commerce',
            'currency' => 'EUR',
            'business_type' => 'products',
            'employees_count' => 3,
            'hours_per_week' => 40,
            'working_days_per_week' => 5,
            'working_days_per_month' => 22,
            'productivity_rate' => 85,
            'monthly_revenue' => 45000,
            'fixed_expenses' => 8500,
            'tax_rate' => 21,
            'price_includes_tax' => true,
            'vat_rate' => 21,
            'sales_commission' => 3,
            'card_fee' => 1.5,
            'other_variable_costs' => 2,
            'investment_rate' => 5,
            'profit_margin' => 15,
            'is_active' => true,
        ]);
        $ecommerce->recalculateMarkup();

        // Produtos do E-commerce
        $this->createProduct($ecommerce, 'Smartphone Samsung Galaxy S24', 'PHONE-001', 'Eletrônicos', [
            ['name' => 'Custo do produto', 'cost' => 650, 'quantity' => 1],
            ['name' => 'Embalagem premium', 'cost' => 5, 'quantity' => 1],
            ['name' => 'Frete de entrada', 'cost' => 8, 'quantity' => 1],
        ]);

        $this->createProduct($ecommerce, 'Notebook Dell Inspiron 15', 'NOTE-001', 'Eletrônicos', [
            ['name' => 'Custo do notebook', 'cost' => 580, 'quantity' => 1],
            ['name' => 'Embalagem', 'cost' => 12, 'quantity' => 1],
            ['name' => 'Frete importação', 'cost' => 25, 'quantity' => 1],
        ]);

        $this->createProduct($ecommerce, 'Fone Bluetooth JBL', 'AUDIO-001', 'Áudio', [
            ['name' => 'Custo do fone', 'cost' => 45, 'quantity' => 1],
            ['name' => 'Embalagem', 'cost' => 2, 'quantity' => 1],
        ]);

        $this->createProduct($ecommerce, 'Smartwatch Apple Watch SE', 'WATCH-001', 'Wearables', [
            ['name' => 'Custo do relógio', 'cost' => 220, 'quantity' => 1],
            ['name' => 'Embalagem premium', 'cost' => 8, 'quantity' => 1],
        ]);

        $this->createProduct($ecommerce, 'Carregador USB-C 65W', 'ACC-001', 'Acessórios', [
            ['name' => 'Custo do carregador', 'cost' => 18, 'quantity' => 1],
            ['name' => 'Embalagem', 'cost' => 1, 'quantity' => 1],
        ]);

        // =====================================================================
        // NEGÓCIO 2: CONSULTORIA DE TI (Apenas Serviços)
        // =====================================================================
        $consultoria = BusinessSetting::create([
            'user_id' => $userId,
            'name' => 'DevPro Consultoria',
            'currency' => 'EUR',
            'business_type' => 'services',
            'employees_count' => 2,
            'hours_per_week' => 40,
            'working_days_per_week' => 5,
            'working_days_per_month' => 22,
            'productivity_rate' => 75,
            'monthly_revenue' => 18000,
            'fixed_expenses' => 3500,
            'tax_rate' => 21,
            'price_includes_tax' => false,
            'vat_rate' => 21,
            'sales_commission' => 0,
            'card_fee' => 0,
            'other_variable_costs' => 1,
            'investment_rate' => 10,
            'profit_margin' => 25,
            'is_active' => true,
        ]);
        $consultoria->recalculateMarkup();

        // Serviços da Consultoria (duração em minutos)
        $this->createService($consultoria, 'Desenvolvimento Web - React', 'DEV-001', 'Desenvolvimento', [
            ['name' => 'Infraestrutura cloud', 'cost' => 5, 'quantity' => 1],
            ['name' => 'Licenças software', 'cost' => 3, 'quantity' => 1],
        ], 480); // 8 horas

        $this->createService($consultoria, 'Consultoria Arquitetura Cloud', 'CONS-001', 'Consultoria', [
            ['name' => 'Ferramentas de análise', 'cost' => 10, 'quantity' => 1],
        ], 240); // 4 horas

        $this->createService($consultoria, 'Manutenção Mensal WordPress', 'MAINT-001', 'Manutenção', [
            ['name' => 'Hosting dedicado', 'cost' => 15, 'quantity' => 1],
            ['name' => 'Backup automático', 'cost' => 5, 'quantity' => 1],
        ], 180); // 3 horas

        $this->createService($consultoria, 'Setup E-commerce Shopify', 'SETUP-001', 'Implementação', [
            ['name' => 'Tema premium', 'cost' => 150, 'quantity' => 1],
            ['name' => 'Apps necessários', 'cost' => 50, 'quantity' => 1],
        ], 1200); // 20 horas

        $this->createService($consultoria, 'Auditoria de Segurança', 'SEC-001', 'Segurança', [
            ['name' => 'Ferramentas de scan', 'cost' => 25, 'quantity' => 1],
            ['name' => 'Relatório detalhado', 'cost' => 10, 'quantity' => 1],
        ], 600); // 10 horas

        // =====================================================================
        // NEGÓCIO 3: GRÁFICA E DESIGN (Produtos + Serviços)
        // =====================================================================
        $grafica = BusinessSetting::create([
            'user_id' => $userId,
            'name' => 'Print & Design Studio',
            'currency' => 'EUR',
            'business_type' => 'both',
            'employees_count' => 4,
            'hours_per_week' => 44,
            'working_days_per_week' => 6,
            'working_days_per_month' => 26,
            'productivity_rate' => 80,
            'monthly_revenue' => 28000,
            'fixed_expenses' => 6200,
            'tax_rate' => 21,
            'price_includes_tax' => true,
            'vat_rate' => 21,
            'sales_commission' => 2,
            'card_fee' => 1.2,
            'other_variable_costs' => 3,
            'investment_rate' => 8,
            'profit_margin' => 20,
            'is_active' => true,
        ]);
        $grafica->recalculateMarkup();

        // Produtos da Gráfica
        $this->createProduct($grafica, 'Cartões de Visita (500 unid)', 'CARD-001', 'Impressos', [
            ['name' => 'Papel couché 300g', 'cost' => 8, 'quantity' => 1],
            ['name' => 'Tinta e impressão', 'cost' => 12, 'quantity' => 1],
            ['name' => 'Corte e acabamento', 'cost' => 5, 'quantity' => 1],
        ]);

        $this->createProduct($grafica, 'Banner Roll-up 85x200cm', 'BANNER-001', 'Grandes Formatos', [
            ['name' => 'Lona impressa', 'cost' => 25, 'quantity' => 1],
            ['name' => 'Estrutura roll-up', 'cost' => 35, 'quantity' => 1],
            ['name' => 'Bolsa de transporte', 'cost' => 8, 'quantity' => 1],
        ]);

        $this->createProduct($grafica, 'Flyers A5 (1000 unid)', 'FLYER-001', 'Impressos', [
            ['name' => 'Papel couché 150g', 'cost' => 15, 'quantity' => 1],
            ['name' => 'Impressão offset', 'cost' => 20, 'quantity' => 1],
        ]);

        $this->createProduct($grafica, 'Catálogo 20 páginas', 'CAT-001', 'Impressos', [
            ['name' => 'Papel interior', 'cost' => 30, 'quantity' => 1],
            ['name' => 'Capa dura', 'cost' => 15, 'quantity' => 1],
            ['name' => 'Encadernação', 'cost' => 10, 'quantity' => 1],
            ['name' => 'Impressão', 'cost' => 45, 'quantity' => 1],
        ]);

        // Serviços da Gráfica
        $this->createService($grafica, 'Design de Logotipo', 'LOGO-001', 'Design', [
            ['name' => 'Banco de imagens', 'cost' => 5, 'quantity' => 1],
            ['name' => 'Fontes premium', 'cost' => 10, 'quantity' => 1],
        ], 360); // 6 horas

        $this->createService($grafica, 'Design de Embalagem', 'PACK-001', 'Design', [
            ['name' => 'Mockups 3D', 'cost' => 15, 'quantity' => 1],
        ], 720); // 12 horas

        $this->createService($grafica, 'Identidade Visual Completa', 'BRAND-001', 'Branding', [
            ['name' => 'Manual de marca', 'cost' => 20, 'quantity' => 1],
            ['name' => 'Arquivos fonte', 'cost' => 10, 'quantity' => 1],
            ['name' => 'Aplicações básicas', 'cost' => 15, 'quantity' => 1],
        ], 1500); // 25 horas

        $this->createService($grafica, 'Design de Post Redes Sociais', 'SOCIAL-001', 'Marketing', [
            ['name' => 'Templates Canva Pro', 'cost' => 2, 'quantity' => 1],
        ], 60); // 1 hora

        // =====================================================================
        // NEGÓCIO 4: WINE HOUSE MADRID (Vinhos com Variantes de Porção)
        // =====================================================================
        $wineHouse = BusinessSetting::create([
            'user_id' => $userId,
            'name' => 'Vinoteca Madrid',
            'currency' => 'EUR',
            'business_type' => 'products',
            'employees_count' => 3,
            'hours_per_week' => 48,
            'working_days_per_week' => 6,
            'working_days_per_month' => 26,
            'productivity_rate' => 90,
            'monthly_revenue' => 35000,
            'fixed_expenses' => 7500,
            'tax_rate' => 21,
            'price_includes_tax' => true,
            'vat_rate' => 21,
            'sales_commission' => 0,
            'card_fee' => 1.5,
            'other_variable_costs' => 3,
            'investment_rate' => 5,
            'profit_margin' => 20,
            'is_active' => true,
        ]);
        $wineHouse->recalculateMarkup();

        // Vinhos Tintos Espanhóis
        $this->createWine($wineHouse, 'Marqués de Riscal Reserva', 'RIOJA-001', 'Tintos - Rioja', 22.50, [
            ['name' => 'Custo da garrafa', 'cost' => 22.50, 'quantity' => 1],
        ]);

        $this->createWine($wineHouse, 'Pesquera Crianza', 'RIBERA-001', 'Tintos - Ribera del Duero', 18.00, [
            ['name' => 'Custo da garrafa', 'cost' => 18.00, 'quantity' => 1],
        ]);

        $this->createWine($wineHouse, 'Protos Roble', 'RIBERA-002', 'Tintos - Ribera del Duero', 12.50, [
            ['name' => 'Custo da garrafa', 'cost' => 12.50, 'quantity' => 1],
        ]);

        $this->createWine($wineHouse, 'Viña Ardanza Reserva', 'RIOJA-002', 'Tintos - Rioja', 28.00, [
            ['name' => 'Custo da garrafa', 'cost' => 28.00, 'quantity' => 1],
        ]);

        $this->createWine($wineHouse, 'Pago de los Capellanes', 'RIBERA-003', 'Tintos - Ribera del Duero', 35.00, [
            ['name' => 'Custo da garrafa', 'cost' => 35.00, 'quantity' => 1],
        ]);

        // Vinhos Brancos Espanhóis
        $this->createWine($wineHouse, 'Albariño Martín Códax', 'RIAS-001', 'Brancos - Rías Baixas', 14.00, [
            ['name' => 'Custo da garrafa', 'cost' => 14.00, 'quantity' => 1],
        ]);

        $this->createWine($wineHouse, 'Rueda Verdejo Oro de Castilla', 'RUEDA-001', 'Brancos - Rueda', 8.50, [
            ['name' => 'Custo da garrafa', 'cost' => 8.50, 'quantity' => 1],
        ]);

        // Champagne & Espumantes
        $this->createWine($wineHouse, 'Cava Codorníu Gran Plus Ultra', 'CAVA-001', 'Espumantes', 24.00, [
            ['name' => 'Custo da garrafa', 'cost' => 24.00, 'quantity' => 1],
        ]);

        $this->createWine($wineHouse, 'Freixenet Carta Nevada', 'CAVA-002', 'Espumantes', 7.50, [
            ['name' => 'Custo da garrafa', 'cost' => 7.50, 'quantity' => 1],
        ]);

        // Vinho do Porto
        $this->createWine($wineHouse, 'Graham\'s 10 Year Tawny', 'PORTO-001', 'Vinhos do Porto', 32.00, [
            ['name' => 'Custo da garrafa', 'cost' => 32.00, 'quantity' => 1],
        ]);

        $this->command->info('Business examples seeded successfully!');
        $this->command->info('- TechStore (Products): ' . $ecommerce->productSheets()->count() . ' products');
        $this->command->info('- DevPro (Services): ' . $consultoria->serviceSheets()->count() . ' services');
        $this->command->info('- Print & Design (Both): ' . $grafica->productSheets()->count() . ' products, ' . $grafica->serviceSheets()->count() . ' services');
        $this->command->info('- Vinoteca Madrid (Wine House): ' . $wineHouse->productSheets()->count() . ' wines with variants');
    }

    /**
     * Create a product with items
     */
    private function createProduct(BusinessSetting $setting, string $name, string $sku, string $category, array $items): ProductSheet
    {
        $product = ProductSheet::create([
            'user_id' => $setting->user_id,
            'business_setting_id' => $setting->id,
            'name' => $name,
            'sku' => $sku,
            'category' => $category,
            'currency' => $setting->currency,
            'is_active' => true,
        ]);

        foreach ($items as $item) {
            ProductSheetItem::create([
                'product_sheet_id' => $product->id,
                'name' => $item['name'],
                'amount' => $item['cost'],
                'quantity' => $item['quantity'],
                'unit_cost' => $item['cost'],
                'type' => 'product_cost',
            ]);
        }

        // Recalculate totals
        $product->refresh();
        $product->cmv_total = $product->items->sum(fn($i) => ($i->amount ?? $i->unit_cost) * $i->quantity);
        $product->sale_price = $product->calculateSalePrice($setting);
        $product->final_price = $product->calculateFinalPrice();
        $product->save();

        return $product;
    }

    /**
     * Create a service with items
     */
    private function createService(BusinessSetting $setting, string $name, string $code, string $category, array $items, int $durationMinutes): ServiceSheet
    {
        // Calculate CSV
        $totalCsv = collect($items)->sum(fn($i) => $i['cost'] * $i['quantity']);
        
        $service = ServiceSheet::create([
            'user_id' => $setting->user_id,
            'business_setting_id' => $setting->id,
            'name' => $name,
            'code' => $code,
            'category' => $category,
            'duration_minutes' => $durationMinutes,
            'total_csv' => $totalCsv,
            'premium_multiplier' => 1.0,
            'psychological_pricing' => false,
            'is_active' => true,
        ]);

        foreach ($items as $item) {
            ServiceSheetItem::create([
                'service_sheet_id' => $service->id,
                'name' => $item['name'],
                'type' => 'supply',
                'unit_cost' => $item['cost'],
                'quantity_used' => $item['quantity'],
            ]);
        }

        // Recalculate pricing
        $service->refresh();
        $service->fixed_cost_portion = $service->calculateFixedCostPortion();
        $service->calculated_price = $service->calculatePrice();
        $service->contribution_margin = $service->calculateContributionMargin();
        $service->margin_percentage = $service->calculated_price > 0 
            ? round(($service->contribution_margin / $service->calculated_price) * 100, 2) 
            : 0;
        $service->save();

        return $service;
    }

    /**
     * Create a wine product with 4 variants: Bottle, Glass, Half Glass, Tasting
     * Standard volumes: Bottle=750ml, Glass=100ml, Half=50ml, Tasting=25ml
     */
    private function createWine(BusinessSetting $setting, string $name, string $sku, string $category, float $bottleCost, array $items): ProductSheet
    {
        // Standard wine volumes
        $baseVolume = 750; // ml per bottle
        $glassVolume = 100; // ml per standard glass (taça)
        
        $product = ProductSheet::create([
            'user_id' => $setting->user_id,
            'business_setting_id' => $setting->id,
            'name' => $name,
            'sku' => $sku,
            'category' => $category,
            'currency' => $setting->currency,
            'base_volume_ml' => $baseVolume,
            'standard_portion_ml' => $glassVolume,
            'is_active' => true,
        ]);

        foreach ($items as $item) {
            ProductSheetItem::create([
                'product_sheet_id' => $product->id,
                'name' => $item['name'],
                'amount' => $item['cost'],
                'quantity' => $item['quantity'],
                'unit_cost' => $item['cost'],
                'type' => 'product_cost',
            ]);
        }

        // Recalculate totals
        $product->refresh();
        $product->cmv_total = $product->items->sum(fn($i) => ($i->amount ?? $i->unit_cost) * $i->quantity);
        $product->sale_price = $product->calculateSalePrice($setting);
        $product->final_price = $product->calculateFinalPrice();
        $product->save();

        // Create 4 variants for wine
        $markup = $setting->markup_calculated ?? 2.5;
        $cmvTotal = $product->cmv_total;
        
        // 1. Garrafa (Bottle) - full unit
        $this->createVariant($product, 'Garrafa', 1.0, 1, $baseVolume, $cmvTotal, $markup, true, 1);
        
        // 2. Taça (Glass) - 100ml = 7.5 glasses per bottle
        $glassRatio = $glassVolume / $baseVolume; // 0.1333
        $glassesPerBottle = $baseVolume / $glassVolume; // 7.5
        $this->createVariant($product, 'Taça', $glassRatio, $glassesPerBottle, $glassVolume, $cmvTotal, $markup, false, 2);
        
        // 3. Meia Taça (Half Glass) - 50ml = 15 per bottle
        $halfGlassVolume = $glassVolume / 2; // 50ml
        $halfGlassRatio = $halfGlassVolume / $baseVolume; // 0.0667
        $halfGlassesPerBottle = $baseVolume / $halfGlassVolume; // 15
        $this->createVariant($product, 'Meia Taça', $halfGlassRatio, $halfGlassesPerBottle, $halfGlassVolume, $cmvTotal, $markup, false, 3);
        
        // 4. Degustação (Tasting) - 25ml = 30 per bottle
        $tastingVolume = $glassVolume / 4; // 25ml
        $tastingRatio = $tastingVolume / $baseVolume; // 0.0333
        $tastingsPerBottle = $baseVolume / $tastingVolume; // 30
        $this->createVariant($product, 'Degustação', $tastingRatio, $tastingsPerBottle, $tastingVolume, $cmvTotal, $markup, false, 4);

        return $product;
    }

    /**
     * Create a product variant
     */
    private function createVariant(ProductSheet $product, string $name, float $portionRatio, float $quantityPerUnit, float $volumeMl, float $cmvTotal, float $markup, bool $isDefault, int $sortOrder): ProductVariant
    {
        $cmvPortion = $cmvTotal * $portionRatio;
        $salePrice = $cmvPortion * $markup;
        $contributionMargin = $salePrice - $cmvPortion;
        $marginPercentage = $salePrice > 0 ? ($contributionMargin / $salePrice) * 100 : 0;
        
        return ProductVariant::create([
            'product_sheet_id' => $product->id,
            'name' => $name,
            'portion_ratio' => $portionRatio,
            'quantity_per_unit' => $quantityPerUnit,
            'volume_ml' => $volumeMl,
            'cmv_portion' => $cmvPortion,
            'markup' => $markup,
            'sale_price' => round($salePrice, 2),
            'contribution_margin' => round($contributionMargin, 2),
            'margin_percentage' => round($marginPercentage, 2),
            'is_default' => $isDefault,
            'sort_order' => $sortOrder,
        ]);
    }
}
