<?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('catalog_price_rules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('organization_id')->constrained('organizations')->cascadeOnDelete();
            $table->string('priceable_type');
            $table->unsignedBigInteger('priceable_id');
            $table->string('name', 120);
            $table->enum('rule_type', ['always', 'weekday', 'date_range', 'specific_date'])->default('always');
            $table->json('weekday_values')->nullable();
            $table->date('start_date')->nullable();
            $table->date('end_date')->nullable();
            $table->date('specific_date')->nullable();
            $table->decimal('customer_price', 12, 2)->nullable();
            $table->decimal('internal_cost', 12, 2)->nullable();
            $table->unsignedSmallInteger('priority')->default(100);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->index(['organization_id', 'priceable_type', 'priceable_id'], 'cpr_priceable_idx');
            $table->index(['is_active', 'priority'], 'cpr_active_priority_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('catalog_price_rules');
    }
};
