<?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('users', function (Blueprint $table) {
            $table->ulid('id')->primary();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('role')->default('user');
            $table->text('bio')->nullable();
            $table->integer('height')->nullable();
            $table->date('date_of_birth')->nullable();
            $table->string('sex')->default('unknown');
            $table->string('physical_activity_level')->default('sedentary');
            $table->string('weight_goal')->default('lose_weight');
            $table->string('pace_preference')->default('slow');
            $table->string('locale', 8)->default('en')->after('email');
            $table->string('avatar_url', 2048)->nullable();

            //            Goals
            $table->unsignedInteger('daily_calorie_goal')->default(2000)->after('avatar_url');
            $table->decimal('daily_protein_goal', 8, 2)->default(120)->after('daily_calorie_goal');
            $table->decimal('daily_carbs_goal', 8, 2)->default(250)->after('daily_protein_goal');
            $table->decimal('daily_fats_goal', 8, 2)->default(70)->after('daily_carbs_goal');
            // Moderation
            $table->timestamp('suspended_at')->nullable()->after('deleted_at')->index();
            $table->ulid('suspended_by')->nullable()->after('suspended_at')->index();
            $table->text('suspended_reason')->nullable()->after('suspended_by');
            $table->rememberToken();
            $table->timestampTz('account_verified_at')->nullable();
            $table->timestamps();
            $table->softDeletesTz();
        });

        Schema::table('users', function (Blueprint $table) {
            $table->foreign('suspended_by')->references('id')->on('users')->nullOnDelete();
        });

        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->foreignUlid('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');
    }
};
