<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // Tabela de campanhas promocionais
        Schema::create('promotional_campaigns', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->string('code', 50)->nullable(); // Código da campanha (ex: BLACKFRIDAY2025)
            $table->text('description')->nullable();
            $table->enum('discount_type', ['percentage', 'fixed', 'price_override'])->default('percentage');
            $table->decimal('discount_value', 10, 2)->default(0); // Valor ou percentual
            $table->date('start_date');
            $table->date('end_date');
            $table->time('start_time')->nullable(); // Hora de início (ex: 00:00:00 para Black Friday)
            $table->time('end_time')->nullable(); // Hora de fim
            $table->boolean('is_active')->default(true);
            $table->boolean('apply_to_all_products')->default(false);
            $table->decimal('min_price', 15, 2)->nullable(); // Preço mínimo durante campanha
            $table->boolean('show_original_price')->default(true); // Mostrar preço riscado
            $table->boolean('show_discount_badge')->default(true); // Mostrar badge de desconto
            $table->string('badge_text', 50)->nullable(); // Texto do badge (ex: "-30%", "BLACK FRIDAY")
            $table->string('badge_color', 20)->default('#ef4444'); // Cor do badge
            $table->integer('priority')->default(0); // Prioridade (maior = prevalece)
            $table->timestamps();
            
            $table->index(['user_id', 'is_active']);
            $table->index(['start_date', 'end_date']);
        });

        // Tabela pivot para produtos na campanha
        Schema::create('campaign_products', function (Blueprint $table) {
            $table->id();
            $table->foreignId('campaign_id')->constrained('promotional_campaigns')->cascadeOnDelete();
            $table->foreignId('product_sheet_id')->constrained('product_sheets')->cascadeOnDelete();
            $table->enum('discount_type', ['percentage', 'fixed', 'price_override'])->nullable(); // Override do desconto
            $table->decimal('discount_value', 10, 2)->nullable(); // Override do valor
            $table->decimal('promotional_price', 15, 2)->nullable(); // Preço calculado
            $table->timestamps();
            
            $table->unique(['campaign_id', 'product_sheet_id']);
        });

        // Adicionar campo de preço promocional na product_sheets
        Schema::table('product_sheets', function (Blueprint $table) {
            $table->decimal('promotional_price', 15, 2)->nullable()->after('final_price');
            $table->foreignId('active_campaign_id')->nullable()->after('promotional_price')
                  ->constrained('promotional_campaigns')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('product_sheets', function (Blueprint $table) {
            $table->dropForeign(['active_campaign_id']);
            $table->dropColumn(['promotional_price', 'active_campaign_id']);
        });
        
        Schema::dropIfExists('campaign_products');
        Schema::dropIfExists('promotional_campaigns');
    }
};
