<?php

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

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->string('sap_order_nr', 18)->unique()->nullable();
            $table->unsignedBigInteger('shopify_id')->unique();
            $table->unsignedBigInteger('customer_id')->nullable();

            $table->dateTime('order_created_at');
            $table->dateTime('order_updated_at')->nullable();
            $table->dateTime('order_processed_at')->nullable();
            $table->dateTime('order_closed_at')->nullable();
            $table->dateTime('order_cancelled_at')->nullable();

            $table->string('email', 255);
            $table->string('fulfillment_status', 50)->nullable();
            $table->string('financial_status', 50);

            $table->string('shipping_name', 255);
            $table->string('shipping_company', 255)->nullable();
            $table->string('shipping_address1', 255);
            $table->string('shipping_address2', 255)->nullable();
            $table->string('shipping_city', 255)->nullable();
            $table->string('shipping_province', 255)->nullable();
            $table->string('shipping_country', 255);
            $table->string('shipping_country_code', 6);
            $table->string('shipping_zip', 50);
            $table->string('shipping_phone', 50)->nullable();

            $table->string('billing_name', 255);
            $table->string('billing_company', 255)->nullable();
            $table->string('billing_address1', 255);
            $table->string('billing_address2', 255)->nullable();
            $table->string('billing_city', 255)->nullable();
            $table->string('billing_province', 255)->nullable();
            $table->string('billing_country', 255);
            $table->string('billing_country_code', 6);
            $table->string('billing_zip', 50);
            $table->string('billing_phone', 50)->nullable();

            $table->boolean('same_address')->default(0);

            $table->string('note', 255)->nullable();
            $table->json('note_attributes')->nullable();
            $table->decimal('total', 15, 2);

            $table->string('sync_hash')->nullable();
            $table->dateTime('last_sync_at')->nullable();
            $table->boolean('to_sync')->default(true);
            $table->timestamps();

            $table->foreign('customer_id')
                ->references('id')->on('customers')
                ->onDelete('SET NULL')->onUpdate('CASCADE');
        });
    }

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