<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('system_settings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('academy_id')->constrained('academies');
            $table->string('group', 50);
            $table->string('key', 100);
            $table->text('value')->nullable();
            $table->string('type', 20)->default('string');
            $table->string('description_ar')->nullable();
            $table->boolean('is_public')->default(false);
            $table->timestamps();

            $table->index(['academy_id', 'group']);
            $table->index(['academy_id', 'key']);
            $table->unique(['academy_id', 'key']);
        });

        DB::statement("ALTER TABLE system_settings ADD CONSTRAINT system_settings_type_check CHECK (type IN ('string', 'integer', 'boolean', 'json', 'float'))");
    }

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