<?php

use App\Models\OrderManagement\Order;
use App\Models\OrderManagement\BillingDesk;
use App\Models\ProductManagement\Product;
use App\Models\StockManagement\Warehouse;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Models\PaymentManagement\PaymentMethod;

return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()  {

        $tableName = (new PaymentMethod)->getTable();

        Schema::whenTableDoesntHaveColumn($tableName, "is_momo",function (Blueprint $table)  {
            $table->boolean('is_momo')
                  ->default(false)
                  ->after("code")
                  ->comment("Determine si c'est un paiement mobile");
        });

        PaymentMethod::whereIn('code',[PaymentMethod::MTN_MOMO_CMR,PaymentMethod::ORANGE_MOMO_CMR])
                    ->update(['is_momo'=>true]);

        Schema::whenTableDoesntHaveColumn((new Product)->getTable(), "ref",function (Blueprint $table)  {
            $table->string('ref')->unique(uniqid("UQ_"))
                ->after("id")
                ->comment("La référence unique du produit");
        });

        Schema::whenTableDoesntHaveColumn((new Order)->getTable(), "billing_desk_id",function (Blueprint $table)  {
            $table->foreignIdFor(BillingDesk::class,'billing_desk_id')->nullable()
                ->constrained((new BillingDesk)->getTable(), (new BillingDesk)->getKeyName())
                ->cascadeOnUpdate()->cascadeOnDelete()
                ->comment("[FK] La caisse associé");

        });

        Schema::whenTableDoesntHaveColumn((new Order)->getTable(), "warehouse_id",function (Blueprint $table)  {
            $table->foreignIdFor(Warehouse::class,'warehouse_id')->nullable()
                ->constrained((new Warehouse)->getTable(), (new Warehouse)->getKeyName())
                ->cascadeOnUpdate()->cascadeOnDelete()
                ->comment("[FK] L'entrepot(magasin/point de ventes) dans le quel les stocks ont été déduit");

        });


    }

};
