<?php

use App\Database\Migrations\TenantMigration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends TenantMigration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('subscriptions', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->unsignedBigInteger('causal_id')->nullable();
            $table->unsignedBigInteger('sub_causal_id')->nullable();
            $table->decimal('subscription_price', 8, 2);
            $table->longText('prices')->nullable();
            $table->boolean('enabled')->default(1);
            $table->softDeletes();
            $table->timestamps();

            $table->foreign('causal_id')->nullable()->references('id')->on('causals')->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('sub_causal_id')->nullable()->references('id')->on('causals')->onUpdate('cascade')->onDelete('cascade');
        });

        Schema::table('records_rows', function (Blueprint $table) {
            $table->unsignedBigInteger('subscription_id')->nullable();
            $table->foreign('subscription_id')->nullable()->references('id')->on('subscriptions')->onUpdate('cascade')->onDelete('cascade');
        });
        Schema::table('receipts_rows', function (Blueprint $table) {
            $table->unsignedBigInteger('subscription_id')->nullable();
            $table->foreign('subscription_id')->nullable()->references('id')->on('subscriptions')->onUpdate('cascade')->onDelete('cascade');
        });


        Schema::create('member_subscriptions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('member_id');
            $table->unsignedBigInteger('subscription_id');
            $table->date('date_from')->nullable();
            $table->date('date_to')->nullable();
            $table->decimal('price', 8, 2);
            $table->unsignedBigInteger('course_subscription_id')->nullable();
            $table->decimal('subscription_price', 8, 2);
            $table->string('notes')->nullable();
            $table->timestamps();

            $table->foreign('member_id')->nullable()->references('id')->on('members')->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('subscription_id')->nullable()->references('id')->on('subscriptions')->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('course_subscription_id')->nullable()->references('id')->on('course_subscriptions')->onUpdate('cascade')->onDelete('cascade');
        });

        Schema::table('rates', function (Blueprint $table) {
            $table->unsignedBigInteger('member_subscription_id')->nullable();
            $table->foreign('member_subscription_id')->nullable()->references('id')->on('member_subscriptions')->onUpdate('cascade')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('rates', function (Blueprint $table) {
            $table->dropColumn('member_subscription_id');
        });

        Schema::dropIfExists('member_subscriptions');


        Schema::table('records_rows', function (Blueprint $table) {
            $table->dropColumn('subscription_id');
        });
        Schema::table('receipts_rows', function (Blueprint $table) {
            $table->dropColumn('subscription_id');
        });

        Schema::dropIfExists('subscriptions');
    }
};
