<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use MtcPharmacy\Subscriptions\Classes\Subscription;

class CreateTablesForSubscriptions extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('subscriptions')) {
            Schema::create('subscriptions', function (Blueprint $table) {
                $table->increments('id');
                $table->string('label', 255)->nullable();
                $table->decimal('price_per_issue')->nullable();
                $table->unsignedInteger('member_id');
                $table->integer('interval_multiplier')->default(1);
                $table->enum('interval', Subscription::get_allowed_intervals())->default(Subscription::INTERVAL_MONTHS);
                $table->enum('status', Subscription::get_allowed_statuses())->default(Subscription::STATUS_PENDING);
                $table->boolean('can_be_paused')->default(true);
                $table->integer('max_number_of_issues')->default(99)->nullable();
                $table->date('next_issue_date')->nullable();
                $table->timestamps();

                $table->foreign('member_id')
                    ->references('id')
                    ->on('members')
                    ->constrained()
                    ->onDelete('restrict')
                    ->onUpdate('cascade');
            });
        }
        if (!Schema::hasTable('subscription_items')) {
            Schema::create('subscription_items', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('subscription_id');
                $table->integer('shop_item_id');
                $table->timestamps();

                $table->foreign('subscription_id')
                    ->references('id')
                    ->on('subscriptions')
                    ->constrained()
                    ->onDelete('cascade')
                    ->onUpdate('cascade');

                $table->foreign('shop_item_id')
                    ->references('id')
                    ->on('items')
                    ->constrained()
                    ->onDelete('restrict')
                    ->onUpdate('cascade');
            });
        }
        if (!Schema::hasTable('subscription_options')) {
            Schema::create('subscription_options', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('item_id')->nullable();
                $table->integer('item_size_id')->nullable();
                $table->boolean('is_allowed')->default(0);
                $table->boolean('can_be_paused')->default(1);
                $table->integer('max_number_of_issues')->default(99)->nullable();
                $table->timestamps();

                $table->foreign('item_id')
                    ->references('id')
                    ->on('items')
                    ->constrained()
                    ->onDelete('cascade')
                    ->onUpdate('cascade');

                $table->foreign('item_size_id')
                    ->references('id')
                    ->on('items_sizes')
                    ->constrained()
                    ->onDelete('cascade')
                    ->onUpdate('cascade');

                $table->unique(['item_id', 'item_size_id']);
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('subscription_options');
        Schema::dropIfExists('subscription_items');
        Schema::dropIfExists('subscriptions');
    }
}
