<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     * 
     * Creates tables for service technical sheets.
     * Similar to product_sheets but for services with time-based pricing.
     */
    public function up(): void
    {
        // Service sheets (fichas técnicas de servicios)
        Schema::create('service_sheets', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('business_setting_id')->constrained()->onDelete('cascade');
            
            // Basic info
            $table->string('name');
            $table->string('code')->nullable(); // Service code/SKU
            $table->string('category')->nullable();
            $table->text('description')->nullable();
            
            // Time configuration
            $table->decimal('duration_minutes', 6, 2)->default(30.00); // Service duration in minutes
            
            // Calculated values (cached)
            $table->decimal('total_csv', 12, 2)->default(0); // Cost of Service Vendido
            $table->decimal('fixed_cost_portion', 12, 2)->default(0); // Gasto fijo por tiempo
            $table->decimal('calculated_price', 12, 2)->default(0);
            $table->decimal('contribution_margin', 12, 2)->default(0);
            $table->decimal('margin_percentage', 5, 2)->default(0);
            
            // Strategic pricing (same as products)
            $table->decimal('competitor_price', 12, 2)->nullable();
            $table->decimal('min_price', 12, 2)->nullable();
            $table->decimal('max_price', 12, 2)->nullable();
            $table->decimal('premium_multiplier', 5, 4)->default(1.0000);
            $table->enum('pricing_strategy', ['aggressive', 'neutral', 'premium'])->default('neutral');
            $table->decimal('target_margin', 5, 2)->nullable();
            $table->boolean('psychological_pricing', )->default(false);
            
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index(['user_id', 'business_setting_id']);
            $table->index(['user_id', 'is_active']);
        });

        // Service sheet items (insumos/componentes del servicio)
        Schema::create('service_sheet_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('service_sheet_id')->constrained()->onDelete('cascade');
            
            $table->string('name'); // Nombre del insumo
            $table->enum('type', [
                'supply',           // Insumos (champú, crema, etc.)
                'consumable',       // Descartables (gillette, guantes, etc.)
                'material',         // Materiales
                'equipment_usage',  // Uso de equipo (amortización)
                'other'
            ])->default('supply');
            
            $table->decimal('unit_cost', 12, 2); // Costo por unidad
            $table->decimal('quantity_used', 10, 4)->default(1); // Cantidad usada por servicio
            $table->string('unit')->nullable(); // ml, g, unidad, etc.
            $table->text('notes')->nullable();
            
            $table->timestamps();
            
            $table->index('service_sheet_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('service_sheet_items');
        Schema::dropIfExists('service_sheets');
    }
};
