<?php

use App\Models\LocalizationManagement\Address;
use App\Models\LocalizationManagement\City;
use App\Models\LocalizationManagement\Country;
use App\Models\LocalizationManagement\ModelHasAddress;
use App\Models\LocalizationManagement\Quarter;
use App\Models\LocalizationManagement\State;
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(){
        Schema::create((new Address)->getTable(), function (Blueprint $table) {
            $table->id();

            $table->string(ModelHasAddress::MORPH_ID_COLUMN,60)->nullable()
                  ->comment("[Polymorph] L'id de l'entité à qui cet addresse est liée Ex: '1'");

            $table->string(ModelHasAddress::MORPH_TYPE_COLUMN,100)->nullable()
                  ->comment("[Polymorph] Le type de l'entité à qui cet addresse est liée Ex: 'user'");

            $table->string('title')->default('Main');

            $table->string('address')->nullable();
            $table->string('address2')->nullable();
            $table->string('contact_full_name')->nullable();
            $table->string('contact_email')->nullable();
            $table->string('contact_phone')->nullable();

            $table->boolean('default')
                ->comment("Determine si c'est l'adresse par défaut")
                ->default(false);

            $table->double('longitude')->nullable();
            $table->double('latitude')->nullable();

            $table->string('zip_code')->nullable();
            $table->string('po_box')->nullable();

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

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

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

            $table->foreignIdFor(Quarter::class,"quarter_id")->nullable()
                ->constrained((new Quarter)->getTable(), (new Quarter)->getKeyName(),uniqid("FK_"))
                ->cascadeOnUpdate()->cascadeOnDelete()
                ->comment("[FK] la ville");

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

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

//        Schema::create((new ModelHasAddress)->getTable(), function (Blueprint $table) {
//
//            $table->id();
//            $table->foreignIdFor(Address::class,'address_id')
//                ->constrained((new Address)->getTable(), (new Address)->getKeyName())
//                ->cascadeOnUpdate()->cascadeOnDelete()
//                ->comment("[FK] l'addresse");
//
//
//        });
    }

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