<?php

use A7kz\Platform\Models\UniModel;
use A7kz\Platform\Modules\Platform\Core\Facades\Core;
use A7kz\Platform\Modules\Platform\Segment\Facades\Segment;
use Carbon\Carbon;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use \A7kz\Platform\Commands\InstallScript;

return new class extends \A7kz\Platform\Commands\InstallScript {

    public function install($module_name, $module_version)
    {
    }

    public function update($module_name, $module_version): void
    {
        $this->upgrade();
    }

    private function upgrade(): void
    {
        $segments = Segment::listActive();
        foreach ($segments as $segment) {
            if (!Schema::connection($segment->connector)->hasTable('pipi_brand_models')) {
                Schema::connection($segment->connector)->create('pipi_brand_models', static function (Blueprint $table) {
                    $table->id();
                    $table->string('name')->nullable()->comment('Наименование');
                    $table->integer('year')->nullable()->comment('Год выпуска');
                    $table->unsignedBigInteger('equipment_id')->nullable('Комплектация');
                    $table->timestamps();
                    $table->softDeletes();

                    $table->unsignedBigInteger('brand_id')->nullable()->comment('Марка');
                    $table->foreign('brand_id')->references('id')->on('pipi_auto_brands');
                });
            }

            if (!Schema::connection($segment->connector)->hasColumn('pipi_brand_models', 'people')) {
                Schema::connection($segment->connector)->table('pipi_brand_models', function ($table) {
                   $table->string('people')->nullable()->comment('Количество мест');
                   $table->string('actuator')->nullable()->comment('Привод');
                   $table->string('fuel_type')->nullable()->comment('Тип топлива');
                   $table->string('hp')->nullable()->comment('Лошадиные силы');
                   $table->string('engine_capacity')->nullable()->comment('Объем двигателя');
                   $table->string('fuel_tank')->nullable()->comment('Объем топливного бака');
                });
            }

            if (!Schema::connection($segment->connector)->hasColumn('pipi_brand_models', 'conditioner')) {
                Schema::connection($segment->connector)->table('pipi_brand_models', function ($table) {
                   $table->boolean('conditioner')->default(true)->comment('Кондиционер');
                });
            }

            if (!Schema::connection($segment->connector)->hasColumn('pipi_brand_models', 'class_id')) {
                Schema::connection($segment->connector)->table('pipi_brand_models', function ($table) {
                    $table->unsignedBigInteger('class_id')->nullable()->comment('Класс');
                });
            }

            if (!Schema::connection($segment->connector)->hasColumn('pipi_brand_models', 'photo_id')) {
                Schema::connection($segment->connector)->table('pipi_brand_models', function ($table) {
                    $table->unsignedBigInteger('photo_id')->nullable()->comment('Фото');
                });
            }
            if (!Schema::connection($segment->connector)->hasColumn('pipi_brand_models', 'bodywork_id')) {
                Schema::connection($segment->connector)->table('pipi_brand_models', function ($table) {
                    $table->unsignedBigInteger('bodywork_id')->nullable()->comment('Кузов');
                });
            }
        }
    }
};
