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

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

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

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

        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('user_role_main');
            $table->string('user_role_local_group');
            $table->string('username')->unique();
            $table->string('password')->nullable();
            $table->string('local_group');
            $table->string('firstname');
            $table->string('nickname')->nullable();
            $table->string('lastname');
            $table->string('membership_id')->nullable();
            $table->string('address_1')->nullable();
            $table->string('address_2')->nullable();
            $table->string('postcode')->nullable();
            $table->string('city')->nullable();
            $table->string('email')->nullable();
            $table->string('phone')->nullable();
            $table->date('birthday')->nullable();
            $table->string('medications')->nullable();
            $table->string('allergies')->nullable();
            $table->string('intolerances')->nullable();
            $table->date('tetanus_vaccination')->nullable();
            $table->string('eating_habits')->nullable();
            $table->string('swimming_permission')->nullable();
            $table->string('first_aid_permission')->nullable();
            $table->string('bank_account_owner')->nullable();
            $table->string('bank_account_iban')->nullable();
            $table->string('activation_token')->nullable();
            $table->dateTime('activation_token_expires_at')->nullable()->default(date('Y-m-d H:i:s', strtotime('+3 days')));
            $table->boolean('active')->default(false);

            $table->foreign('local_group')->references('slug')->on('tenants')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreign('user_role_main')->references('slug')->on('user_roles')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreign('user_role_local_group')->references('slug')->on('user_roles')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreign('swimming_permission')->references('slug')->on('swimming_permissions')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreign('eating_habits')->references('slug')->on('eating_habits')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreign('first_aid_permission')->references('slug')->on('first_aid_permissions')->restrictOnDelete()->cascadeOnUpdate();

             $table->rememberToken();
            $table->timestamps();
        });

        Schema::create('password_reset_tokens', function (Blueprint $table) {
            $table->string('email')->primary();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('sessions', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->foreignId('user_id')->nullable()->index();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->longText('payload');
            $table->integer('last_activity')->index();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('users');
        Schema::dropIfExists('password_reset_tokens');
        Schema::dropIfExists('sessions');
        Schema::dropIfExists('user_roles');
        Schema::dropIfExists('eating_habits');
        Schema::dropIfExists('swimming_permissions');
        Schema::dropIfExists('first_aid_permissions');
    }
};
