<?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
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->foreignId('user_id')->primary()->constrained('users')->cascadeOnDelete();
            $table->string('display_name')->default('');
            $table->string('avatar')->default('🙂');
            $table->timestamp('onboarding_completed_at')->nullable();
            $table->string('onboarding_variant')->nullable();
            $table->string('onboarding_group_id')->nullable();
            $table->uuid('default_space_id')->nullable();
            $table->timestamp('updated_at')->useCurrent();
        });

        Schema::create('user_snapshots', function (Blueprint $table) {
            $table->foreignId('user_id')->primary()->constrained('users')->cascadeOnDelete();
            $table->json('snapshot');
            $table->timestamp('updated_at')->useCurrent();
        });

        Schema::create('spaces', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('name')->default('My space');
            $table->foreignId('created_by')->constrained('users')->cascadeOnDelete();
            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->useCurrent();
            $table->index('created_by');
        });

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

        Schema::create('space_members', function (Blueprint $table) {
            $table->uuid('space_id');
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('role')->default('member');
            $table->timestamp('created_at')->useCurrent();
            $table->primary(['space_id', 'user_id']);
            $table->foreign('space_id')->references('id')->on('spaces')->cascadeOnDelete();
            $table->index('user_id');
        });

        Schema::create('people', function (Blueprint $table) {
            $table->string('id', 64)->primary();
            $table->uuid('space_id');
            $table->foreignId('auth_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('email')->default('');
            $table->string('name')->default('');
            $table->string('avatar')->default('🙂');
            $table->timestamp('created_at')->useCurrent();
            $table->foreign('space_id')->references('id')->on('spaces')->cascadeOnDelete();
            $table->index('space_id');
        });

        Schema::create('groups', function (Blueprint $table) {
            $table->string('id', 64)->primary();
            $table->uuid('space_id');
            $table->string('name');
            $table->string('emoji')->default('📁');
            $table->string('type')->default('home');
            $table->string('currency')->default('USD');
            $table->decimal('total_expenses', 18, 2)->default(0);
            $table->date('created_at')->useCurrent();
            $table->foreign('space_id')->references('id')->on('spaces')->cascadeOnDelete();
            $table->index('space_id');
        });

        Schema::create('group_members', function (Blueprint $table) {
            $table->string('group_id', 64);
            $table->string('person_id', 64);
            $table->primary(['group_id', 'person_id']);
            $table->foreign('group_id')->references('id')->on('groups')->cascadeOnDelete();
            $table->foreign('person_id')->references('id')->on('people')->cascadeOnDelete();
        });

        Schema::create('shared_lists', function (Blueprint $table) {
            $table->string('id', 64)->primary();
            $table->uuid('space_id');
            $table->string('name');
            $table->string('emoji')->default('📝');
            $table->date('created_at')->useCurrent();
            $table->foreign('space_id')->references('id')->on('spaces')->cascadeOnDelete();
            $table->index('space_id');
        });

        Schema::create('shared_list_members', function (Blueprint $table) {
            $table->string('shared_list_id', 64);
            $table->string('person_id', 64);
            $table->primary(['shared_list_id', 'person_id']);
            $table->foreign('shared_list_id')->references('id')->on('shared_lists')->cascadeOnDelete();
            $table->foreign('person_id')->references('id')->on('people')->cascadeOnDelete();
        });

        Schema::create('shared_list_items', function (Blueprint $table) {
            $table->string('id', 64)->primary();
            $table->string('shared_list_id', 64);
            $table->string('item_text')->default('');
            $table->boolean('done')->default(false);
            $table->string('added_by_person_id', 64)->nullable();
            $table->foreign('shared_list_id')->references('id')->on('shared_lists')->cascadeOnDelete();
            $table->foreign('added_by_person_id')->references('id')->on('people')->nullOnDelete();
        });

        Schema::create('shared_purchases', function (Blueprint $table) {
            $table->string('id', 64)->primary();
            $table->uuid('space_id');
            $table->string('name');
            $table->string('emoji')->default('🛒');
            $table->string('status')->default('planning');
            $table->text('notes');
            $table->decimal('target_amount', 18, 2)->nullable();
            $table->date('created_at')->useCurrent();
            $table->foreign('space_id')->references('id')->on('spaces')->cascadeOnDelete();
            $table->index('space_id');
        });

        Schema::create('shared_purchase_members', function (Blueprint $table) {
            $table->string('shared_purchase_id', 64);
            $table->string('person_id', 64);
            $table->primary(['shared_purchase_id', 'person_id']);
            $table->foreign('shared_purchase_id')->references('id')->on('shared_purchases')->cascadeOnDelete();
            $table->foreign('person_id')->references('id')->on('people')->cascadeOnDelete();
        });

        Schema::create('expenses', function (Blueprint $table) {
            $table->string('id', 64)->primary();
            $table->uuid('space_id');
            $table->string('group_id', 64)->default('');
            $table->string('shared_list_id', 64)->nullable();
            $table->text('description');
            $table->decimal('amount', 18, 2)->default(0);
            $table->string('currency')->default('USD');
            $table->string('paid_by_person_id', 64);
            $table->string('split_type')->default('equal');
            $table->json('split_among');
            $table->string('category')->default('');
            $table->string('emoji')->default('💰');
            $table->date('expense_date')->useCurrent();
            $table->boolean('is_recurring')->default(false);
            $table->foreign('space_id')->references('id')->on('spaces')->cascadeOnDelete();
            $table->foreign('paid_by_person_id')->references('id')->on('people')->restrictOnDelete();
            $table->foreign('shared_list_id')->references('id')->on('shared_lists')->nullOnDelete();
            $table->index('space_id');
            $table->index('group_id');
        });

        Schema::create('activities', function (Blueprint $table) {
            $table->string('id', 64)->primary();
            $table->uuid('space_id');
            $table->string('activity_type');
            $table->text('description');
            $table->json('i18n')->nullable();
            $table->string('user_person_id', 64);
            $table->decimal('amount', 18, 2)->nullable();
            $table->string('group_name')->nullable();
            $table->string('list_name')->nullable();
            $table->timestamp('activity_at')->useCurrent();
            $table->foreign('space_id')->references('id')->on('spaces')->cascadeOnDelete();
            $table->foreign('user_person_id')->references('id')->on('people')->cascadeOnDelete();
            $table->index('space_id');
        });

        Schema::create('group_invitations', function (Blueprint $table) {
            $table->string('id', 64)->primary();
            $table->uuid('space_id');
            $table->string('group_id', 64);
            $table->string('invited_email')->default('');
            $table->string('invited_name')->default('');
            $table->string('invited_person_id', 64);
            $table->string('status')->default('pending');
            $table->string('invited_by_person_id', 64);
            $table->timestamp('created_at')->useCurrent();
            $table->foreign('space_id')->references('id')->on('spaces')->cascadeOnDelete();
            $table->foreign('group_id')->references('id')->on('groups')->cascadeOnDelete();
            $table->foreign('invited_person_id')->references('id')->on('people')->cascadeOnDelete();
            $table->foreign('invited_by_person_id')->references('id')->on('people')->cascadeOnDelete();
            $table->index('space_id');
            $table->index('group_id');
            $table->index('invited_email');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('group_invitations');
        Schema::dropIfExists('activities');
        Schema::dropIfExists('expenses');
        Schema::dropIfExists('shared_purchase_members');
        Schema::dropIfExists('shared_purchases');
        Schema::dropIfExists('shared_list_items');
        Schema::dropIfExists('shared_list_members');
        Schema::dropIfExists('shared_lists');
        Schema::dropIfExists('group_members');
        Schema::dropIfExists('groups');
        Schema::dropIfExists('people');
        Schema::dropIfExists('space_members');
        Schema::dropIfExists('spaces');
        Schema::dropIfExists('user_snapshots');
        Schema::dropIfExists('profiles');
    }
};

