<?php
declare(strict_types=1);

use Celena\Core\Database\Blueprint;
use Celena\Core\Database\Connection;
use Celena\Core\Database\Schema;

/**
 * Ядро: добавляет таблицы для баннеров, меню сайта и городов.
 * Каждая таблица создаётся только если её ещё нет.
 */
return function (Schema $schema, Connection $conn): void {
    if (!$schema->hasTable('celena_banners')) {
        $schema->create('celena_banners', function (Blueprint $t) {
            $t->id();
            $t->string('title', 255);
            $t->string('image', 500)->nullable();
            $t->string('mobile_image', 500)->nullable();
            $t->string('url', 500)->nullable();
            // hero | sidebar | footer | popup
            $t->string('placement', 50)->default('hero');
            $t->text('subtitle')->nullable();
            $t->text('cta_label')->nullable();
            $t->datetime('starts_at')->nullable();
            $t->datetime('ends_at')->nullable();
            $t->integer('sort_order')->default(0);
            $t->boolean('is_active')->default(true);
            $t->datetime('created_at')->nullable();
            $t->datetime('updated_at')->nullable();
            $t->index('placement');
            $t->index('is_active');
        });
    }

    if (!$schema->hasTable('celena_menus')) {
        $schema->create('celena_menus', function (Blueprint $t) {
            $t->id();
            $t->string('slug', 100);
            $t->string('title', 200);
            $t->datetime('created_at')->nullable();
            $t->datetime('updated_at')->nullable();
            $t->unique('slug');
        });
    }

    if (!$schema->hasTable('celena_menu_items')) {
        $schema->create('celena_menu_items', function (Blueprint $t) {
            $t->id();
            $t->bigInteger('menu_id');
            $t->bigInteger('parent_id')->nullable();
            $t->string('title', 255);
            $t->string('url', 500);
            // page | news | category | external | divider
            $t->string('kind', 30)->default('external');
            $t->bigInteger('object_id')->nullable();
            $t->string('target', 20)->default('_self');
            $t->string('icon', 50)->nullable();
            $t->integer('sort_order')->default(0);
            $t->boolean('is_active')->default(true);
            $t->datetime('created_at')->nullable();
            $t->index('menu_id');
            $t->index('parent_id');
        });
    }

    if (!$schema->hasTable('celena_cities')) {
        $schema->create('celena_cities', function (Blueprint $t) {
            $t->id();
            $t->string('slug', 100);
            $t->string('title', 200);
            $t->string('country', 100)->nullable();
            $t->string('region', 200)->nullable();
            $t->string('timezone', 64)->nullable();
            $t->string('phone', 32)->nullable();
            $t->decimal('latitude', 10, 6)->nullable();
            $t->decimal('longitude', 10, 6)->nullable();
            $t->boolean('is_default')->default(false);
            $t->boolean('is_active')->default(true);
            $t->integer('sort_order')->default(0);
            $t->datetime('created_at')->nullable();
            $t->datetime('updated_at')->nullable();
            $t->unique('slug');
            $t->index('is_active');
        });
    }
};
