<?php

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

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->nullable();
            $table->string('sku')->nullable();
            $table->float('price', 15, 3)->nullable();
            $table->float('discount_amount', 5, 2)->nullable();
            $table->float('discount_price', 15, 3)->nullable();
            $table->string('thumbnail')->nullable();
            $table->string('weight')->nullable();
            $table->string('dimensions')->nullable();
            $table->text('short_description')->nullable();
            $table->longText('description')->nullable();
            $table->longText('information')->nullable();
            $table->text('note')->nullable();
            $table->integer('in_stock')->default(0);
            $table->float('rating_avg', 5, 3)->default(0);
            $table->integer('total_review')->default(0);
            $table->integer('total_share')->default(0);
            $table->integer('total_view')->default(0);
            $table->boolean('is_activated')->default(1);
            $table->boolean('is_deleted')->default(0);
            $table->timestamps();
        });

        Schema::create('product_categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->nullable();
            $table->tinyText('description')->nullable();
            $table->integer('parent_id')->default(0);
            $table->boolean('is_activated')->default(1);
            $table->boolean('is_deleted')->default(0);
            $table->timestamps();
        });
        Schema::create('product_category_details', function (Blueprint $table) {
            $table->id();
            $table->integer('product_id');
            $table->integer('product_category_id');
            $table->timestamps();
        });

        Schema::create('product_brands', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->nullable();
            $table->tinyText('description')->nullable();
            $table->integer('parent_id')->default(0);
            $table->boolean('is_activated')->default(1);
            $table->boolean('is_deleted')->default(0);
            $table->timestamps();
        });
        Schema::create('product_brand_details', function (Blueprint $table) {
            $table->id();
            $table->integer('product_id');
            $table->integer('product_brand_id');
            $table->timestamps();
        });

        Schema::create('product_sizes', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->nullable();
            $table->tinyText('description')->nullable();
            $table->integer('parent_id')->default(0);
            $table->boolean('is_activated')->default(1);
            $table->boolean('is_deleted')->default(0);
            $table->timestamps();
        });
        Schema::create('product_size_details', function (Blueprint $table) {
            $table->id();
            $table->integer('product_id');
            $table->integer('product_size_id');
            $table->timestamps();
        });

        Schema::create('product_colors', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->nullable();
            $table->tinyText('description')->nullable();
            $table->integer('parent_id')->default(0);
            $table->boolean('is_activated')->default(1);
            $table->boolean('is_deleted')->default(0);
            $table->timestamps();
        });
        Schema::create('product_color_details', function (Blueprint $table) {
            $table->id();
            $table->integer('product_id');
            $table->integer('product_color_id');
            $table->timestamps();
        });

        //one-many
        Schema::create('product_images', function (Blueprint $table) {
            $table->id();
            $table->integer('product_id');
            $table->string('file_name');
            $table->string('name')->nullable();
            $table->timestamps();
        });

        //one-many
        Schema::create('product_reviews', function (Blueprint $table) {
            $table->id();
            $table->integer('product_id');
            $table->string('name')->nullable();
            $table->string('email')->nullable();
            $table->text('content')->nullable();
            $table->float('rating', 5, 3)->default(0);
            $table->boolean('is_activated')->default(1);
            $table->boolean('is_deleted')->default(0);
            $table->timestamps();
        });

        Schema::create('product_discounts', function (Blueprint $table) {
            $table->id();
            $table->string('code');
            $table->float('percent', 5, 2)->nullable();
            $table->float('amount', 15, 3)->nullable();
            $table->dateTime('start_at')->nullable();
            $table->dateTime('end_at')->nullable();
            $table->integer('limit')->nullable();
            $table->boolean('is_activated')->default(1);
            $table->boolean('is_deleted')->default(0);
            $table->timestamps();
        });
    }

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

        Schema::dropIfExists('product_categories');
        Schema::dropIfExists('product_category_details');

        Schema::dropIfExists('product_brands');
        Schema::dropIfExists('product_brand_details');

        Schema::dropIfExists('product_sizes');
        Schema::dropIfExists('product_size_details');

        Schema::dropIfExists('product_colors');
        Schema::dropIfExists('product_color_details');

        Schema::dropIfExists('product_images');
        Schema::dropIfExists('product_reviews');
        Schema::dropIfExists('product_discounts');
    }
}
