<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->string('nav_no', 20)->unique()->nullable();
            $table->string('shopify_gid', 100)->unique();
            $table->string('number', 20)->unique();
            $table->unsignedBigInteger('customer_id')->nullable();
            $table->unsignedBigInteger('company_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_first_name', 255)->nullable();
            $table->string('shipping_last_name', 255)->nullable();
            $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_province_code', 6)->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_first_name', 255)->nullable();
            $table->string('billing_last_name', 255)->nullable();
            $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_province_code', 6)->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->string('shipping_type', 255)->nullable();
            $table->decimal('shipping_total', 15, 2)->default(0);
            $table->decimal('shipping_discount', 15, 2)->default(0);
            $table->decimal('total', 15, 2)->default(0);
            $table->string('currency', 10)->nullable();
            $table->decimal('shipping_to_refund', 15, 2)->default(0);
            $table->decimal('refunded_shipping', 15, 2)->default(0);
            $table->boolean('no_transaction')->default(0);
            $table->boolean('accepts_marketing')->default(false);

            $table->dateTime('last_sync_at')->nullable();
            $table->boolean('to_sync')->default(true);
            $table->dateTime('last_sync_status_at')->nullable();
            $table->boolean('to_sync_status')->default(false);
            $table->timestamps();

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

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

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