<?php

use A7kz\Platform\Modules\Platform\Segment\Facades\Segment;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;


return new class extends Migration
{
    public function up(): void
    {
        $segments = Segment::listActive();
        foreach ($segments as $segment) {
            Schema::connection($segment->connector)->create('pipi_auto', static function (Blueprint $table) {
                $table->id();
                $table->string('code')->unique()->comment('Код');
                $table->string('name')->nullable()->comment('Наименование');
                $table->unsignedBigInteger('type_id')->nullable()->comment('Тип');
                $table->unsignedBigInteger('class_id')->nullable()->comment('Класс');
                $table->unsignedBigInteger('brand_id')->nullable()->comment('Марка');
                $table->unsignedBigInteger('model_id')->nullable()->comment('Модель');
                $table->unsignedBigInteger('color_id')->nullable()->comment('Цвет');
                $table->string('serial_number')->nullable()->comment('Серийный номер');
                $table->string('state_number')->nullable()->comment('Госномер');
                $table->date('manufacture_year')->nullable()->comment('Год производства');
                $table->string('passport_number')->nullable()->comment('Номер техпаспорта');
                $table->date('passport_date')->nullable()->comment('Дата техпаспорта');
                $table->float('estimated_cost')->nullable()->comment('Оценочная стоимость');
                $table->unsignedBigInteger('owner_id')->nullable()->comment('Владелец имущества');
                $table->boolean('is_trust_management')->default(false)->comment('В доверенном управлении');
                $table->integer('owner_percentage')->nullable()->comment('Процент владельца');
                $table->string('owner_contract')->nullable()->comment('Договор владельца имущества');
                $table->boolean('is_inactive')->default(false)->comment('Не использовать');
                $table->json('inspection_kit')->nullable()->comment('Комплектация для осмотра');
                $table->boolean('is_predefined')->default(false)->comment('Предопределенный');
                $table->string('predefined_data_name')->nullable()->comment('Имя предопределенных данных');
                $table->timestamps();
                $table->softDeletes();

                // Внешние ключи
                $table->foreign('type_id')->references('id')->on('pipi_auto_types');
                $table->foreign('class_id')->references('id')->on('pipi_auto_classes');
                $table->foreign('brand_id')->references('id')->on('pipi_auto_brands');
                $table->foreign('model_id')->references('id')->on('pipi_brand_models');
                $table->foreign('color_id')->references('id')->on('pipi_auto_colors');
                $table->foreign('owner_id')->references('id')->on('pipi_owners');
            });
        }
    }
};
