<?php

use App\Models\OrderManagement\BillingDesk;
use App\Models\ProductManagement\Category;
use App\Models\LocalizationManagement\City;
use App\Models\LocalizationManagement\Country;
use App\Models\OrderManagement\Coupon;
use App\Models\SecurityManagement\Role;
use App\Models\OrderManagement\Order;
use App\Models\PaymentManagement\Payment;
use App\Models\PaymentManagement\PaymentMethod;
use App\Models\ProductManagement\Product;
use App\Models\OrderManagement\ShippingCompany;
use App\Models\LocalizationManagement\State;
use App\Models\SettingManagement\StatusChange;
use App\Models\StockManagement\Adjustment;
use App\Models\StockManagement\StockRequest;
use App\Models\StockManagement\Importation;
use App\Models\StockManagement\Stock;
use App\Models\StockManagement\Warehouse;
use App\Models\ProductManagement\Tag;
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(){

        //class pour slug-column
        $classes = [
            Role::class, Admin::class, Customer::class,
            Country::class, State::class, City::class,
            Order::class, Product::class, Tag::class, Category::class, ShippingCompany::class, Coupon::class,
            Payment::class, PaymentMethod::class,
            Stock::class, Warehouse::class, Importation::class, StockRequest::class, Adjustment::class,
            StatusChange::class, BillingDesk::class,
        ];

        $authorableOptions = config("eloquent-authorable");
        $createdByColumnName = $authorableOptions['created_by_column_name'] ?? "created_by";
        $updatedByColumnName = $authorableOptions['updated_by_column_name'] ?? "updated_by";

        foreach ($classes as $class) {
            $model = (new $class);
            $authorable = $model->authorable;
            //$createdByColumnName = $authorable['created_by_column_name'] ?? $createdByColumnName;
            $setUpdatedByColumnName = $authorable['created_by_column_name'] ?? true;
            $setCreatedByColumn = $authorable['updated_by_column_name'] ?? true;

            if ($setCreatedByColumn and Schema::hasTable($model->getTable()) ){
                Schema::whenTableDoesntHaveColumn($model->getTable(), $createdByColumnName,function (Blueprint $table) use ($createdByColumnName,$authorableOptions) {
//                $table->addAuthorableColumns(true, User::class)

                    $table->foreignIdFor($authorableOptions['users_model'],$createdByColumnName)->nullable()
                        ->constrained((new $authorableOptions['users_model'])->getTable(), (new $authorableOptions['users_model'])->getKeyName(), uniqid("FK_"))
                        ->cascadeOnUpdate()->cascadeOnDelete()
                        ->comment("[FK] l'auteur de l'enregistrement");
                });
            }
            if ($setUpdatedByColumnName and Schema::hasTable($model->getTable())){
                Schema::whenTableDoesntHaveColumn($model->getTable(), $updatedByColumnName,function (Blueprint $table) use ($updatedByColumnName,$authorableOptions) {

                    $table->foreignIdFor($authorableOptions['users_model'],$updatedByColumnName)->nullable()
                        ->constrained((new $authorableOptions['users_model'])->getTable(), (new $authorableOptions['users_model'])->getKeyName(), uniqid("FK_"))
                        ->cascadeOnUpdate()->cascadeOnDelete()
                        ->comment("[FK] l'auteur de la dernière modification");

                });
            }

            Schema::whenTableDoesntHaveColumn((new Order)->getTable(), "note",function (Blueprint $table) {
                $table->longText('note')->nullable()->comment("une note");
            });

            Schema::whenTableDoesntHaveColumn((new StatusChange)->getTable(), "message",function (Blueprint $table) {
                $table->longText('message')->nullable()->comment("un message rattaché à ce change de statut");
            });

            Schema::whenTableHasColumn((new Customer)->getTable(), "password",function (Blueprint $table) {
               // $table->string('password')->nullable()->change();
            });



        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
};
