<?php

use App\Models\LocalizationManagement\Address;
use App\Models\LocalizationManagement\City;
use App\Models\OrderManagement\Order;
use App\Models\OrderManagement\BillingDesk;
use App\Models\OrderManagement\BillingDeskHasUser;
use App\Models\OrderManagement\ShippingCompany;
use App\Models\PaymentManagement\PaymentMethod;
use App\Models\SettingManagement\Status;
use App\Models\UserManagement\Admin;
use App\Models\UserManagement\Customer;
use Illuminate\Database\Migrations\Migration;
use App\Helpers\DatabaseSchema\Schema;
use App\Helpers\DatabaseSchema\CustomBluePrint as Blueprint;

return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(){
        if (!Schema::hasTable((new BillingDesk)->getTable())) {
            Schema::create((new BillingDesk)->getTable(), function (Blueprint $table) {
                $table->id();
                $table->string('code')->nullable()->unique()
                    ->comment("Le code unique");

                $table->string('name', 100)->nullable()
                    ->comment("Le nom");

                $table->foreignIdFor(City::class,'city_id')->nullable()
                    ->constrained((new City)->getTable(), (new City)->getKeyName())
                    ->cascadeOnUpdate()->cascadeOnDelete()
                    ->comment("[FK]");

                $table->boolean('active')->default(true);

                $table->timestamps();
                $table->softDeletes();
            });
        }


        if (!Schema::hasTable((new BillingDeskHasUser)->getTable())) {
            Schema::create((new BillingDeskHasUser)->getTable(), function (Blueprint $table) {
                $table->id();

                $table->foreignIdFor(BillingDesk::class,'billing_desk_id')
                    ->constrained((new BillingDesk)->getTable(), (new BillingDesk)->getKeyName())
                    ->cascadeOnUpdate()->cascadeOnDelete()
                    ->comment("[FK]");

                $table->foreignIdFor(Admin::class,'manager_id')
                    ->constrained((new Admin)->getTable(), (new Admin)->getKeyName())
                    ->cascadeOnUpdate()->cascadeOnDelete()
                    ->comment("[FK]");

            });
        }



        Schema::create((new Order)->getTable(), function (Blueprint $table) {
            $table->id();
            $table->string('ref_id')->nullable()->unique();

            $table->foreignIdFor(Customer::class,'customer_id')
                ->constrained((new Customer)->getTable(), (new Customer)->getKeyName())
                ->cascadeOnUpdate()->cascadeOnDelete()
                ->comment("[FK]");

            $table->foreignIdFor(Address::class,'address_id')->nullable()
                ->constrained((new Address)->getTable(), (new Address)->getKeyName())
                ->cascadeOnUpdate()->cascadeOnDelete()
                ->comment("[FK]");

            $table->foreignIdFor(ShippingCompany::class,'shipping_company_id')->nullable()
                ->constrained((new ShippingCompany)->getTable(), (new ShippingCompany)->getKeyName())
                ->cascadeOnUpdate()->cascadeOnDelete()
                ->comment("[FK]");


            $table->foreignIdFor(PaymentMethod::class,'payment_method_id')->nullable()
                ->constrained((new PaymentMethod)->getTable(), (new PaymentMethod)->getKeyName())
                ->cascadeOnUpdate()->cascadeOnDelete()
                ->comment("[FK]");

            $table->string('period',20)->nullable();
            $table->string('discount_code')->nullable();
            $table->double('discount')->default(0);
            $table->double('subtotal')->default(0);
            $table->double('shipping')->default(0);
            $table->double('tax')->default(0);
            $table->double('total')->default(0);
            $table->string('currency')->default(getSettingsOf("currency"));

            $table->longText('note')->nullable()->comment("une note");

            $table->foreignIdFor(Status::class,'status_code')
                ->default(Status::UNPROCESSED)
                ->constrained((new Status)->getTable(), (new Status)->getKeyName())
                ->cascadeOnUpdate()->cascadeOnDelete()
                ->comment("[FK]");

         //   $table->string('status_code',100)->default(Status::NEW_ORDER);
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(){
        Schema::dropIfExists((new Order)->getTable());
        Schema::dropIfExists((new BillingDeskHasUser)->getTable());
        Schema::dropIfExists((new BillingDesk)->getTable());
    }
};
