<?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::create('participation_fee_types', function (Blueprint $table) {
            $table->string('slug')->primary();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('participation_types', function (Blueprint $table) {
            $table->string('slug')->primary();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('event_participation_fees', function (Blueprint $table) {
           $table->id();
           $table->string('tenant');
           $table->string('type');
           $table->string('name');
           $table->string('description')->nullable()->default('');
           $table->float('amount_standard',2);
           $table->float('amount_reduced',2)->nullable();
           $table->float('amount_solidarity', 2)->nullable();
           $table->timestamps();

            $table->foreign('type')->references('slug')->on('participation_types')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreign('tenant')->references('slug')->on('tenants')->restrictOnDelete()->cascadeOnUpdate();
        });

        Schema::create('events', function (Blueprint $table) {
            $table->id();
            $table->string('tenant');
            $table->foreignId('cost_unit_id')->nullable()->constrained('cost_units', 'id')->restrictOnDelete()->cascadeOnUpdate();

            $table->string('name');
            $table->string('identifier')->unique();
            $table->string('location');
            $table->string('postal_code');
            $table->string('email');
            $table->date('start_date');
            $table->date('end_date');
            $table->date('early_bird_end');
            $table->date('registration_final_end');
            $table->integer('early_bird_end_amount_increase');
            $table->string('account_owner');
            $table->string('account_iban');
            $table->boolean('registration_allowed')->default(false);
            $table->string('participation_fee_type');
            $table->float('total_max_amount', 2)->default(0);
            $table->string('registration_link')->nullable();
            $table->boolean('pay_per_day');
            $table->boolean('pay_direct');
            $table->boolean('sibling_reduction')->default(false);
            $table->boolean('send_weekly_report')->default(true);
            $table->foreignId('participation_fee_1')->nullable()->constrained('event_participation_fees', 'id')->nullOnDelete()->cascadeOnUpdate();
            $table->foreignId('participation_fee_2')->nullable()->constrained('event_participation_fees', 'id')->nullOnDelete()->cascadeOnUpdate();
            $table->foreignId('participation_fee_3')->nullable()->constrained('event_participation_fees', 'id')->nullOnDelete()->cascadeOnUpdate();
            $table->foreignId('participation_fee_4')->nullable()->constrained('event_participation_fees', 'id')->nullOnDelete()->cascadeOnUpdate();
            $table->float('support_per_person', 2)->default(0);
            $table->float('support_flat', 2)->default(0);
            $table->integer('alcoholics_age')->default(16);
            $table->boolean('archived')->default(false);
            $table->timestamps();

            $table->foreign('tenant')->references('slug')->on('tenants')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreign('participation_fee_type')->references('slug')->on('participation_fee_types')->restrictOnDelete()->cascadeOnUpdate();
        });

        Schema::create('event_allowed_eating_habits', function (Blueprint $table) {
            $table->id();
            $table->foreignId('event_id')->constrained('events', 'id')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreignId('eating_habit_id')->constrained('eating_habits', 'id')->restrictOnDelete()->cascadeOnUpdate();
            $table->timestamps();
        });

        Schema::create('event_managers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('event_id')->constrained('events', 'id')->restrictOnDelete()->cascadeOnUpdate();
            $table->string('user_id')->constrained('users', 'id')->restrictOnDelete()->cascadeOnUpdate();
            $table->timestamps();
        });


    }







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