<?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::table('users', function (Blueprint $table): void {
            $table->string('role')->default('user')->after('password');
            $table->timestamp('suspended_at')->nullable()->after('remember_token');
        });

        Schema::create('plans', function (Blueprint $table): void {
            $table->id();
            $table->string('slug')->unique();
            $table->string('name');
            $table->unsignedInteger('price_cents')->default(0);
            $table->string('interval', 32)->default('monthly');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('plan_features', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('plan_id')->constrained()->cascadeOnDelete();
            $table->string('feature_key');
            $table->unsignedInteger('limit')->nullable();
            $table->timestamps();

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

        Schema::create('saas_subscriptions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('plan_id')->constrained()->restrictOnDelete();
            $table->string('status', 32)->default('active');
            $table->timestamp('trial_ends_at')->nullable();
            $table->timestamp('current_period_start')->nullable();
            $table->timestamp('current_period_end')->nullable();
            $table->timestamp('canceled_at')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'status']);
        });

        Schema::create('usage_counters', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('metric_key');
            $table->string('period', 16);
            $table->unsignedInteger('count')->default(0);
            $table->timestamps();

            $table->unique(['user_id', 'metric_key', 'period']);
        });

        Schema::create('user_preferences', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->json('data');
            $table->timestamps();

            $table->unique('user_id');
        });

        Schema::create('transactions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('external_id');
            $table->string('name');
            $table->string('category_id', 64);
            $table->decimal('amount', 15, 2);
            $table->date('date');
            $table->string('type', 32)->default('expense');
            $table->boolean('is_recurring')->default(false);
            $table->string('recurring_frequency', 32)->nullable();
            $table->date('recurring_start_date')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->unique(['user_id', 'external_id']);
            $table->index(['user_id', 'date']);
        });

        Schema::create('budgets', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('external_id');
            $table->string('name');
            $table->string('category_id', 64);
            $table->decimal('limit_amount', 15, 2)->default(0);
            $table->decimal('spent_amount', 15, 2)->default(0);
            $table->string('period', 32)->default('monthly');
            $table->string('icon', 16)->nullable();
            $table->timestamps();

            $table->unique(['user_id', 'external_id']);
            $table->index('user_id');
        });

        Schema::create('goals', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('external_id');
            $table->string('name');
            $table->decimal('target_amount', 15, 2)->default(0);
            $table->decimal('saved_amount', 15, 2)->default(0);
            $table->string('icon', 16)->nullable();
            $table->date('deadline')->nullable();
            $table->decimal('monthly_contribution', 15, 2)->default(0);
            $table->string('priority', 32)->default('medium');
            $table->timestamps();

            $table->unique(['user_id', 'external_id']);
            $table->index('user_id');
        });

        Schema::create('debts', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('external_id');
            $table->string('name');
            $table->decimal('balance', 15, 2)->default(0);
            $table->decimal('original_balance', 15, 2)->default(0);
            $table->decimal('interest_rate', 8, 2)->default(0);
            $table->decimal('minimum_payment', 15, 2)->default(0);
            $table->unsignedTinyInteger('due_day')->nullable();
            $table->string('icon', 16)->nullable();
            $table->string('type', 32)->default('loan');
            $table->timestamps();

            $table->unique(['user_id', 'external_id']);
            $table->index('user_id');
        });

        Schema::create('subscription_items', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('external_id');
            $table->string('name');
            $table->string('category_id', 64)->default('subscriptions');
            $table->decimal('cost', 15, 2)->default(0);
            $table->string('billing_cycle', 32)->default('monthly');
            $table->date('next_renewal')->nullable();
            $table->date('last_charge')->nullable();
            $table->string('status', 32)->default('active');
            $table->unsignedTinyInteger('confidence')->nullable();
            $table->unsignedTinyInteger('usage_score')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->unique(['user_id', 'external_id']);
            $table->index(['user_id', 'status']);
        });

        Schema::create('resource_shares', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('owner_user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('shared_with_user_id')->constrained('users')->cascadeOnDelete();
            $table->morphs('shareable');
            $table->string('permission', 16)->default('view');
            $table->timestamps();

            $table->unique(['shared_with_user_id', 'shareable_type', 'shareable_id'], 'resource_shares_unique');
            $table->index(['owner_user_id', 'shareable_type']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('resource_shares');
        Schema::dropIfExists('subscription_items');
        Schema::dropIfExists('debts');
        Schema::dropIfExists('goals');
        Schema::dropIfExists('budgets');
        Schema::dropIfExists('transactions');
        Schema::dropIfExists('user_preferences');
        Schema::dropIfExists('usage_counters');
        Schema::dropIfExists('saas_subscriptions');
        Schema::dropIfExists('plan_features');
        Schema::dropIfExists('plans');

        Schema::table('users', function (Blueprint $table): void {
            $table->dropColumn(['role', 'suspended_at']);
        });
    }
};
