<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('material_prices', function (Blueprint $table) {
            $table->enum('member_type', ['umum', 'studio'])->default('umum')->after('material_id');
            
            // Drop FK first because it relies on the index
            $table->dropForeign(['material_id']);
            
            // Update index to include member_type for faster lookups
            $table->dropIndex(['material_id', 'sisi', 'min_qty']);
            $table->index(['material_id', 'member_type', 'sisi', 'min_qty']);
            
            // Re-add FK
            $table->foreign('material_id')->references('id')->on('materials')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('material_prices', function (Blueprint $table) {
            $table->dropForeign(['material_id']);
            
            $table->dropIndex(['material_id', 'member_type', 'sisi', 'min_qty']);
            $table->dropColumn('member_type');
            
            $table->index(['material_id', 'sisi', 'min_qty']);
            $table->foreign('material_id')->references('id')->on('materials')->onDelete('cascade');
        });
    }
};
