<?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_applications')) {
                Schema::connection($segment->connector)->create('pipi_applications', static function (Blueprint $table) {
                    $table->id();
                    $table->unsignedBigInteger('car_id')->nullable();
                    $table->unsignedBigInteger('user_id')->nullable();
                    $table->integer('rent_day')->nullable();
                    $table->string('phone')->nullable();
                    $table->dateTime('started_at')->nullable();
                    $table->dateTime('ended_at')->nullable();
                    $table->string('status')->nullable();
                    $table->string('user_name')->nullable();
                    $table->string('user_surname')->nullable();
                    $table->string('user_email')->nullable();
                    $table->timestamps();
                    $table->softDeletes();
                });
            }

            if (!Schema::connection($segment->connector)->hasColumn('pipi_applications', 'photos')) {
                Schema::connection($segment->connector)->table('pipi_applications', static function (Blueprint $table) {
                    $table->json('photos')->nullable();
                });
            }

            if (!Schema::connection($segment->connector)->hasColumn('pipi_applications', 'address_start')) {
                Schema::connection($segment->connector)->table('pipi_applications', static function (Blueprint $table) {
                    $table->unsignedBigInteger('address_end')->nullable();
                    $table->unsignedBigInteger('address_start')->nullable();
                });
            }

            if (!Schema::connection($segment->connector)->hasColumn('pipi_applications', 'sum')) {
                Schema::connection($segment->connector)->table('pipi_applications', static function (Blueprint $table) {
                    $table->integer('sum')->nullable();
                });
            }

            if (!Schema::connection($segment->connector)->hasColumn('pipi_applications', 'deposit')) {
                Schema::connection($segment->connector)->table('pipi_applications', static function (Blueprint $table) {
                    $table->integer('deposit')->nullable();
                });
            }
        }
    }
};
