<?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
    {
        // Ana tür tablosu
        Schema::create('species', function (Blueprint $table) {
            $table->id();
            $table->string('slug')->unique();
            $table->string('scientific_name')->unique();
            $table->string('family')->nullable();
            $table->string('tr_name');
            $table->string('en_name')->nullable();
            $table->integer('fishbase_id')->nullable();
            $table->integer('worms_aphia_id')->nullable();
            $table->string('photo_url')->nullable();
            $table->text('summary_md')->nullable();
            $table->boolean('is_migratory')->default(false);
            $table->string('primary_water_zone')->nullable(); // coastal, neritic, pelagic
            $table->string('profile_status')->default('draft'); // draft, reviewed, published
            $table->integer('data_version')->default(1);
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index('profile_status');
            $table->index('slug');
        });

        // Tür tercih profili — generic değerlendirme sistemi
        Schema::create('species_feature_prefs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('species_id')->constrained('species')->cascadeOnDelete();
            $table->string('feature_key'); // depth_m, substrate_type, wind_speed, vb.
            $table->string('eval_type');   // range, categorical, distance_bonus, distance_penalty, trend, gradient, presence, value_in_set
            $table->jsonb('params');       // tip-spesifik parametreler
            $table->decimal('weight', 3, 2)->default(0.5); // 0..1
            $table->string('note')->nullable(); // editör notu
            $table->timestamps();

            $table->unique(['species_id', 'feature_key']);
            $table->index('feature_key');
        });

        // Predator-prey ilişkisi
        Schema::create('species_prey', function (Blueprint $table) {
            $table->id();
            $table->foreignId('predator_id')->constrained('species')->cascadeOnDelete();
            $table->foreignId('prey_species_id')->constrained('species')->cascadeOnDelete();
            $table->decimal('strength', 3, 2)->default(0.5); // 0..1
            $table->string('context')->nullable(); // mevsim/bölge filtresi
            $table->timestamps();

            $table->unique(['predator_id', 'prey_species_id']);
        });

        // Bölgesel tercih override'ları
        Schema::create('species_regional_overrides', function (Blueprint $table) {
            $table->id();
            $table->foreignId('species_id')->constrained('species')->cascadeOnDelete();
            $table->string('region_code'); // ege, marmara, akdeniz, karadeniz
            $table->string('feature_key');
            $table->string('eval_type');
            $table->jsonb('params');
            $table->decimal('weight', 3, 2)->default(0.5);
            $table->timestamps();

            $table->unique(['species_id', 'region_code', 'feature_key']);
        });

        // Çeviri tablosu
        Schema::create('species_translations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('species_id')->constrained('species')->cascadeOnDelete();
            $table->string('lang', 5); // tr, en, de, ru
            $table->string('name');
            $table->text('summary')->nullable();
            $table->jsonb('alt_names')->nullable(); // ["İzmarit", "Karaburun"]
            $table->timestamps();

            $table->unique(['species_id', 'lang']);
        });

        // Revizyon geçmişi
        Schema::create('species_revisions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('species_id')->constrained('species')->cascadeOnDelete();
            $table->integer('revision_no');
            $table->jsonb('snapshot'); // profilin tam fotoğrafı
            $table->string('author')->nullable();
            $table->text('change_note')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->unique(['species_id', 'revision_no']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('species_revisions');
        Schema::dropIfExists('species_translations');
        Schema::dropIfExists('species_regional_overrides');
        Schema::dropIfExists('species_prey');
        Schema::dropIfExists('species_feature_prefs');
        Schema::dropIfExists('species');
    }
};
