<?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
    {
        Schema::create('plans', function (Blueprint $table) {
            $table->id();
            $table->foreignId('network_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('category_id')->constrained()->cascadeOnDelete();
            $table->foreignId('force_vendor_id')->nullable()->constrained('vendors')->nullOnDelete();
            $table->string('name');
            $table->string('value')->nullable();
            $table->decimal('selling_price', 14, 2);
            $table->enum('status', ['available', 'hidden', 'maintenance'])->default('available');
            $table->unsignedInteger('sort_order')->default(0);
            $table->json('meta_json')->nullable();
            $table->timestamps();

            $table->index(['category_id', 'status']);
            $table->index(['network_id', 'category_id']);
        });

        Schema::create('plan_vendor_mappings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('plan_id')->constrained()->cascadeOnDelete();
            $table->foreignId('vendor_id')->constrained()->cascadeOnDelete();
            $table->string('vendor_plan_code');
            $table->unsignedInteger('priority')->default(1);
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->boolean('force_vendor')->default(false);
            $table->json('meta_json')->nullable();
            $table->timestamps();

            $table->unique(['plan_id', 'vendor_id']);
            $table->unique(['plan_id', 'priority']);
            $table->index(['vendor_id', 'status']);
        });

        Schema::create('service_vendor_mappings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('category_id')->constrained()->cascadeOnDelete();
            $table->foreignId('network_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('vendor_id')->constrained()->cascadeOnDelete();
            $table->unsignedInteger('priority')->default(1);
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->boolean('force_vendor')->default(false);
            $table->json('meta_json')->nullable();
            $table->timestamps();

            $table->index(['category_id', 'network_id', 'status', 'priority'], 'svm_category_network_status_priority_idx');
            $table->index(['vendor_id', 'status']);
        });

        Schema::create('markup_rules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('category_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('network_id')->nullable()->constrained()->nullOnDelete();
            $table->string('tier')->nullable();
            $table->enum('rule_type', ['fixed', 'percent']);
            $table->decimal('rule_value', 14, 2);
            $table->unsignedInteger('priority')->default(1);
            $table->boolean('is_active')->default(true);
            $table->json('meta_json')->nullable();
            $table->timestamps();

            $table->index(['is_active', 'priority']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('markup_rules');
        Schema::dropIfExists('service_vendor_mappings');
        Schema::dropIfExists('plan_vendor_mappings');
        Schema::dropIfExists('plans');
    }
};

