<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateNewTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('categories');
        Schema::dropIfExists('manufacturers');
        Schema::dropIfExists('category_manufacturers');
        Schema::dropIfExists('products');
        Schema::dropIfExists('product_notes');
        Schema::dropIfExists('product_purchase_methods');
        Schema::dropIfExists('product_images');
        Schema::dropIfExists('product_options');
        Schema::dropIfExists('product_option_details');
        Schema::dropIfExists('product_details');
        Schema::dropIfExists('product_detail_option_details');
        Schema::dropIfExists('product_reviews');
        Schema::dropIfExists('delivery_addresses');
        Schema::dropIfExists('orders');
        Schema::dropIfExists('order_details');
        Schema::dropIfExists('payments');
        Schema::dropIfExists('user_wishlists');
        Schema::dropIfExists('sliders');
        Schema::dropIfExists('contacts');
        Schema::dropIfExists('order_by_mail_forms');
        Schema::dropIfExists('article_categories');
        Schema::dropIfExists('article_keywords');
        Schema::dropIfExists('articles');
        Schema::dropIfExists('article_category_details');
        Schema::dropIfExists('article_keyword_details');
        Schema::dropIfExists('seo_metas');
        Schema::dropIfExists('pages');

        //Schema::dropIfExists('users');

        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug');
            $table->string('image')->nullable();
            $table->integer('parent_id')->default(0);
            $table->string('description')->nullable();
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();
        });


        Schema::create('manufacturers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug');
            $table->string('image')->nullable();
            $table->string('description')->nullable();
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();
        });

        Schema::create('category_manufacturers', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('category_id')->nullable();
            $table->unsignedBigInteger('manufacturer_id')->nullable();
            $table->timestamps();

            //$table->foreign('category_id')->references('id')->on('categories');
            //$table->foreign('manufacturer_id')->references('id')->on('manufacturers');
        });

        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('category_id')->nullable();
            $table->string('sku');
            $table->string('name');
            $table->string('slug');
            $table->string('image')->nullable();
            $table->text('description')->nullable();
            $table->double('price_from', 15, 3)->nullable();
            $table->double('price_to', 15, 3)->nullable();
            $table->float('discount', 5, 2)->nullable();
            $table->dateTime('discount_start_time')->nullable();
            $table->dateTime('discount_end_time')->nullable();
            $table->double('regular_price_from', 15, 3)->nullable();
            $table->double('regular_price_to', 15, 3)->nullable();
            $table->text('note')->nullable();
            $table->integer('possible_delivery_time')->nullable();
            $table->smallInteger('is_available_in_stock')->default(1);
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();

            //$table->foreign('category_id')->references('id')->on('categories');
        });

        Schema::create('product_notes', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id')->nullable();
            $table->text('content');
            $table->smallInteger('is_important')->default(0);
            $table->timestamps();

            //$table->foreign('product_id')->references('id')->on('products');
        });

        Schema::create('product_purchase_methods', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id')->nullable();
            $table->string('name');
            $table->string('slug')->nullable();
            $table->string('description')->nullable();
            $table->timestamps();

            //$table->foreign('product_id')->references('id')->on('products');
        });

        Schema::create('product_images', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id')->nullable();
            $table->string('name');
            $table->string('file_name');
            $table->integer('position')->default(0);
            $table->timestamps();

            //$table->foreign('product_id')->references('id')->on('products');
        });

        Schema::create('product_options', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id')->nullable();
            $table->string('name');
            $table->string('slug');
            $table->string('title')->nullable();
            $table->string('description')->nullable();
            $table->integer('position')->default(0);
            $table->smallInteger('allow_option')->default(1);
            $table->timestamps();

            //$table->foreign('product_id')->references('id')->on('products');
        });

        Schema::create('product_option_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_option_id')->nullable();
            $table->string('name');
            $table->string('slug');
            $table->string('image')->nullable();
            $table->string('description')->nullable();
            $table->integer('position')->default(0);
            $table->timestamps();

            //$table->foreign('product_option_id')->references('id')->on('product_options');
        });

        Schema::create('product_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id')->nullable();
            $table->string('sku');
            $table->double('price', 15, 3)->nullable();
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->integer('position')->default(0);
            $table->timestamps();

            //$table->foreign('product_id')->references('id')->on('products');
        });

        Schema::create('product_detail_option_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_detail_id')->nullable();
            $table->unsignedBigInteger('product_option_detail_id')->nullable();
            $table->timestamps();

            //$table->foreign('product_detail_id')->references('id')->on('product_details');
            //$table->foreign('product_option_detail_id')->references('id')->on('product_option_details');
        });

        Schema::create('payments', function (Blueprint $table) {
            $table->id();
            $table->smallInteger('type')->default(0);
            $table->string('provider');
            $table->string('code');
            $table->double('total', 15, 3)->nullable();
            $table->text('content')->nullable();
            $table->text('error_content')->nullable();
            $table->dateTime('finished_at')->nullable();
            $table->smallInteger('status')->default(0);
            $table->timestamps();
        });

        Schema::create('product_reviews', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id')->nullable();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->float('rate', 3, 2)->nullable();
            $table->text('comment')->nullable();
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();

            //$table->foreign('product_id')->references('id')->on('products');
            //$table->foreign('user_id')->references('id')->on('users');
        });

        Schema::create('delivery_addresses', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('furigana_first_name');
            $table->string('furigana_last_name');
            $table->string('post_code');
            $table->string('district')->nullable();
            $table->string('city')->nullable();
            $table->string('address');
            $table->string('phone_number');
            $table->string('email');
            $table->unsignedBigInteger('user_id')->nullable();
            $table->smallInteger('is_default')->default(0);
            $table->timestamps();

            //$table->foreign('user_id')->references('id')->on('users');
        });

        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->string('type');
            $table->unsignedBigInteger('user_id')->nullable();
            $table->unsignedBigInteger('delivery_address_id')->nullable();
            $table->double('total_price', 15, 3)->nullable();
            $table->dateTime('estimated_delivery_date')->nullable();
            $table->smallInteger('status')->default(0);
            $table->smallInteger('payment_status')->default(0);
            $table->unsignedBigInteger('payment_id')->nullable();
            $table->string('payment_code')->nullable();
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();

            //$table->foreign('user_id')->references('id')->on('users');
            //$table->foreign('delivery_address_id')->references('id')->on('delivery_addresses');
            //$table->foreign('payment_id')->references('id')->on('payments');
        });

        Schema::create('order_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('order_id')->nullable();
            $table->unsignedBigInteger('product_id')->nullable();
            $table->unsignedBigInteger('product_detail_id')->nullable();
            $table->string('product_name')->nullable();
            $table->string('product_image')->nullable();
            $table->string('order_method')->nullable();
            $table->float('discount', 5, 2)->nullable();
            $table->double('catalog_price', 15, 3)->nullable();
            $table->double('price', 15, 3)->nullable();
            $table->double('shipping_cost', 15, 3)->nullable();
            $table->integer('quantity');
            $table->double('total_price', 15, 3)->nullable();
            $table->timestamps();

            //$table->foreign('order_id')->references('id')->on('orders');
            //$table->foreign('product_id')->references('id')->on('products');
            //$table->foreign('product_detail_id')->references('id')->on('product_details');
        });

        Schema::create('user_wishlists', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->unsignedBigInteger('product_id')->nullable();
            $table->timestamps();

            //$table->foreign('user_id')->references('id')->on('users');
            //$table->foreign('product_id')->references('id')->on('products');
        });

        Schema::create('sliders', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('title')->nullable();;
            $table->string('content')->nullable();;
            $table->string('file_name')->nullable();
            $table->smallInteger('position')->default(0);
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();
        });

        Schema::create('contacts', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email');
            $table->string('phone_number');
            $table->string('content')->nullable();
            $table->smallInteger('status')->default(0);
            $table->timestamps();
        });

        Schema::create('order_by_mail_forms', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('phone_number');
            $table->string('email');
            $table->string('code')->nullable();
            $table->string('attach_file')->nullable();
            $table->text('question')->nullable();
            $table->smallInteger('status')->default(0);
            $table->timestamps();
        });

        Schema::create('article_categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug');
            $table->integer('parent_id')->default(0);
            $table->string('description')->nullable();
            $table->integer('position')->default(0);
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();
        });

        Schema::create('article_keywords', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug');
            $table->string('description')->nullable();
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();
        });

        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('slug');
            $table->text('description')->nullable();
            $table->string('editor')->nullable();;
            $table->text('gu_content')->nullable();;
            $table->text('content')->nullable();;
            $table->string('thumbnail')->nullable();;
            $table->string('preview')->nullable();;
            $table->dateTime('publish_at')->nullable();;
            $table->smallInteger('important')->nullable();
            $table->integer('view_number')->default(0);
            $table->string('type')->nullable();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->smallInteger('is_published')->default(0);
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();

            //$table->foreign('user_id')->references('id')->on('users');
        });

        Schema::create('article_category_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('article_id')->nullable();
            $table->unsignedBigInteger('article_category_id')->nullable();
            $table->timestamps();

            //$table->foreign('article_id')->references('id')->on('articles');
            //$table->foreign('article_category_id')->references('id')->on('article_categories');
        });

        Schema::create('article_keyword_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('article_id')->nullable();
            $table->unsignedBigInteger('article_keyword_id')->nullable();
            $table->timestamps();

            //$table->foreign('article_id')->references('id')->on('articles');
            //$table->foreign('article_keyword_id')->references('id')->on('article_keywords');
        });

        Schema::create('seo_metas', function (Blueprint $table) {
            $table->id();
            $table->string('uri');
            $table->string('title')->nullable();
            $table->text('keywords')->nullable();
            $table->text('description')->nullable();
            $table->string('image')->nullable();
            $table->string('type')->nullable();
            $table->integer('post_id')->nullable();
            $table->timestamps();
        });


        Schema::create('pages', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('slug');
            $table->text('description')->nullable();
            $table->longText('content')->nullable();
            $table->string('thumbnail')->nullable();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();

            //$table->foreign('user_id')->references('id')->on('users');
        });


        //Change User table
        /*Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('username');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('furigana_first_name')->nullable();
            $table->string('furigana_last_name')->nullable();

            $table->string('city')->nullable();
            $table->string('district')->nullable();
            $table->integer('point')->default(0);

            $table->smallInteger('is_activated')->default(1);
            $table->smallInteger('is_deleted')->default(0);
            $table->timestamps();
        });*/

        Schema::table('users', function (Blueprint $table) {
            $table->string('username')->nullable()->after('furigana_name');
            $table->string('first_name')->nullable()->after('furigana_name');
            $table->string('last_name')->nullable()->after('furigana_name');
            $table->string('furigana_first_name')->nullable()->after('furigana_name');
            $table->string('furigana_last_name')->nullable()->after('furigana_name');

            $table->string('city')->nullable()->after('post_code');
            $table->string('district')->nullable()->after('post_code');
            $table->integer('point')->default(0)->after('phone_number');

            $table->smallInteger('is_activated')->default(1)->after('login_url');
            $table->smallInteger('is_deleted')->default(0)->after('login_url');

            $table->dropColumn('qualification');
            $table->dropColumn('career');
            $table->dropColumn('contact_method');
            $table->dropColumn('counseling_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
        Schema::dropIfExists('manufacturers');
        Schema::dropIfExists('category_manufacturers');
        Schema::dropIfExists('products');
        Schema::dropIfExists('product_notes');
        Schema::dropIfExists('product_purchase_methods');
        Schema::dropIfExists('product_images');
        Schema::dropIfExists('product_options');
        Schema::dropIfExists('product_option_details');
        Schema::dropIfExists('product_details');
        Schema::dropIfExists('product_detail_option_details');
        Schema::dropIfExists('product_reviews');
        Schema::dropIfExists('delivery_addresses');
        Schema::dropIfExists('orders');
        Schema::dropIfExists('order_details');
        Schema::dropIfExists('payments');
        Schema::dropIfExists('user_wishlists');
        Schema::dropIfExists('sliders');
        Schema::dropIfExists('contacts');
        Schema::dropIfExists('order_by_mail_forms');
        Schema::dropIfExists('article_categories');
        Schema::dropIfExists('article_keywords');
        Schema::dropIfExists('articles');
        Schema::dropIfExists('article_category_details');
        Schema::dropIfExists('article_keyword_details');
        Schema::dropIfExists('seo_metas');
        Schema::dropIfExists('pages');

        //Schema::dropIfExists('users');

        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('username');
            $table->dropColumn('first_name');
            $table->dropColumn('last_name');
            $table->dropColumn('furigana_first_name');
            $table->dropColumn('furigana_last_name');
            $table->dropColumn('city');
            $table->dropColumn('district');
            $table->dropColumn('point');
            $table->dropColumn('is_activated');
            $table->dropColumn('is_deleted');

            $table->string('qualification')->nullable();
            $table->string('career')->nullable();
            $table->string('contact_method')->nullable();
            $table->string('counseling_id')->nullable();
        });
    }
}
