<?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();
        $this->seed();
    }

    private function upgrade(): void
    {
        $segments = Segment::listActive();
        foreach ($segments as $segment) {
            if (!Schema::connection($segment->connector)->hasTable('pipi_auto_tariffs')) {
                Schema::connection($segment->connector)->create('pipi_auto_tariffs', static function (Blueprint $table) {
                    $table->id();
                    $table->string('name');
                    $table->string('type');
                    $table->integer('day_range_start');
                    $table->integer('day_range_end');
                    $table->integer('base_rate');
                    $table->integer('deposit');
                    $table->timestamps();
                    $table->softDeletes();

                    $table->unsignedBigInteger('model_id')->nullable()->comment('Тариф');
                    $table->foreign('model_id')->references('id')->on('pipi_brand_models');
                });
            }
        }
    }

    private function seed()
    {
        $count = UniModel::model('pipi_auto_tariffs')->count();
        if ($count === 0) {
            $seed = Storage::disk('pipicar_crm')->get('auto_tariffs/seeds/seed.json');

            $data = json_decode($seed, true);
            foreach ($data as $item) {
                $models = explode(' ', $item['model']);
                if (isset($models[1])) {
                    $model = $models[1];
                    if (isset($models[2])) {
                        $model = $models[1] . ' ' . $models[2];
                    }
                }

                foreach ($item['rental_periods'] as $rental) {
                    UniModel::model('pipi_auto_tariffs')->create([
                        'name' => $item['model'] . '-' . $item['manufacture_year'] . ' ' . $rental['min_days'] . '-' . $rental['max_days'],
                        'type' => 'dynamic',
                        'day_range_start' => $rental['min_days'],
                        'day_range_end' => $rental['max_days'],
                        'base_rate' => $rental['rate_may_oct'],
                        'deposit' => $rental['deposit'],
                        'model_id' => UniModel::model('pipi_brand_models')
                            ->where('name', $model)->where('year', $item['manufacture_year'])->first()?->id,
                    ]);
                }
            }
        }
    }
};
