<?php

declare(strict_types=1);

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

return new class extends Migration {

    public function up(): void
    {
        Schema::create('subscription_plans', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hashkey', 300)->unique();

            $table->string('name')->index();
            $table->string('description')->nullable();
            $table->decimal('price', 15, 2)->default(0);
            $table->unsignedInteger('duration_days')->default(30);
            // 'restrict' | 'warn' | 'auto_deduct'
            $table->string('expiry_action')->default('warn');
            $table->boolean('active')->default(true)->index();
            $table->json('additional_details')->nullable();

            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->foreign('created_by')->references('id')->on('users')->nullOnDelete();
            $table->foreign('updated_by')->references('id')->on('users')->nullOnDelete();

            $table->timestamps();
        });


        Schema::create('subscriptions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hashkey', 300)->unique();

            $table->unsignedBigInteger('user_id')->index();
            $table->unsignedBigInteger('plan_id');
            $table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
            $table->foreign('plan_id')->references('id')->on('subscription_plans')->cascadeOnDelete();

            // 'active' | 'expired' | 'cancelled' | 'pending'
            $table->string('status')->default('pending')->index();
            $table->dateTime('starts_at')->nullable();
            $table->dateTime('expires_at')->nullable()->index();

            // 'wallet' | 'gcash' | 'paymaya' — extensible for future gateways
            $table->string('payment_method')->default('wallet');
            $table->json('additional_details')->nullable();

            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->foreign('created_by')->references('id')->on('users')->nullOnDelete();
            $table->foreign('updated_by')->references('id')->on('users')->nullOnDelete();

            $table->timestamps();
        });


        Schema::create('subscription_invoices', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hashkey', 300)->unique();

            $table->unsignedBigInteger('subscription_id');
            $table->unsignedBigInteger('user_id')->index();
            $table->foreign('subscription_id')->references('id')->on('subscriptions')->cascadeOnDelete();
            $table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();

            $table->decimal('amount', 15, 2)->default(0);
            // 'pending' | 'paid' | 'failed'
            $table->string('status')->default('pending')->index();
            $table->dateTime('paid_at')->nullable();

            // 'wallet' | 'gcash' | 'paymaya'
            $table->string('payment_method')->default('wallet');
            // external gateway transaction ID, QR reference, etc.
            $table->string('payment_reference')->nullable()->index();

            $table->json('additional_details')->nullable();

            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->foreign('created_by')->references('id')->on('users')->nullOnDelete();
            $table->foreign('updated_by')->references('id')->on('users')->nullOnDelete();

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

    public function down(): void
    {
        Schema::dropIfExists('subscription_invoices');
        Schema::dropIfExists('subscriptions');
        Schema::dropIfExists('subscription_plans');
    }
};
