<?php

declare(strict_types=1);

namespace Plugin\tnt_stabila_shop\Migrations;

use JTL\Plugin\Migration;
use JTL\Update\IMigration;
use Plugin\tnt_stabila_shop\src\Enums\PluginSettings;
use Plugin\tnt_stabila_shop\src\Enums\SettingType;

/**
 * Class Migration20250616090235
 *  create initial database schema
 *
 * @author sssr
 * @created Mon, 16 Jun 2025 09:02:35 +0100
 * /
 */
class Migration20250616090235 extends Migration implements IMigration
{
    protected $author      = 'sssr';
    protected $description = 'create initial database schema';

    public function getAuthor(): ?string
    {
        return 'sssr';
    }

    public function getDescription(): string
    {
        return 'create initial database schema';
    }

    /**
     * @inheritdoc
     */
    public function up(): void
    {
        $this->execute('CREATE TABLE `stabila_shop_events` (
            `id`            INT UNSIGNED NOT NULL AUTO_INCREMENT,
            `exceptionType` VARCHAR(255) NOT NULL DEFAULT \'\',
            `sdkVersion`    VARCHAR(8) NOT NULL,
            `extra`         JSON NOT NULL,
            `filePath`      VARCHAR(255) NOT NULL DEFAULT \'\',
            `functionName`  VARCHAR(255) NOT NULL DEFAULT \'\',
            `template`      VARCHAR(255) NOT NULL DEFAULT \'\',
            `shopVersion`   VARCHAR(5) NOT NULL DEFAULT \'\',
            `severity`      VARCHAR(32) NOT NULL DEFAULT \'\',
            `message`       VARCHAR(255) NOT NULL DEFAULT \'\',
            `varDump`       TEXT NOT NULL DEFAULT \'\',
            `hash`          VARCHAR(32) NOT NULL DEFAULT \'\',
            `createDate`    DATETIME DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (`id`),
            KEY `stabila_shop_KEY_exception_type_idx` (`exceptionType`)
            ) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');

        $this->execute('CREATE TABLE `stabila_shop_performance` (
            `id`                        INT UNSIGNED NOT NULL AUTO_INCREMENT,
            `pageType`                  INT NOT NULL,
            `totalExecutionInSeconds`   DOUBLE NOT NULL,
            `createDate`                DATETIME NOT NULL,
            PRIMARY KEY (`id`),
            KEY `stabila_shop_KEY_page_type_idx` (`pageType`)
            ) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');

        $this->execute('CREATE TABLE `stabila_shop_settings` (
            `id`            INT UNSIGNED NOT NULL AUTO_INCREMENT,
            `settingType`   INT NOT NULL,
            `settingName`   VARCHAR(255) NOT NULL,
            `settingValue`  VARCHAR(255) NOT NULL,
            PRIMARY KEY (`id`),
            KEY `stabila_shop_KEY_settings_idx` (`settingName`)
            ) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');

        // Insert default settings
        foreach (PluginSettings::cases() as $pluginSetting) {
            try {
                $setting = \json_decode(
                    $pluginSetting->value,
                    false,
                    512,
                    JSON_THROW_ON_ERROR
                );
            } catch (\JsonException) {
                continue;
            }
            /** @var object{name: string, type: string, defaultValue: string} $setting */
            $this->db->insertRow(
                'stabila_shop_settings',
                (object)[
                    'settingType'  => $setting->type,
                    'settingName'  => $setting->name,
                    'settingValue' => $setting->defaultValue,
                ],
            );
        }

        $this->execute('CREATE TABLE `stabila_shop_sales_channel` (
            `orderID`           INT NOT NULL,
            `referrer`          VARCHAR(255) NOT NULL,
            `totalOrderValue`   DOUBLE NOT NULL,
            PRIMARY KEY (`orderID`),
            KEY `stabila_shop_KEY_sales_channel_idx` (`referrer`)
            ) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');

        $this->execute('CREATE TABLE `stabila_shop_notifications` (
            `type`          INT NOT NULL,
            `title`         VARCHAR(255) NOT NULL,
            `description`   TEXT NOT NULL,
            `hash`          VARCHAR(40) NOT NULL,
            `createDate`    DATETIME NOT NULL,
            PRIMARY KEY (`hash`)
            ) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');
    }

    /**
     * @inheritdoc
     */
    public function down(): void
    {
        $this->execute('DROP TABLE IF EXISTS `stabila_shop_events`');
        $this->execute('DROP TABLE IF EXISTS `stabila_shop_performance`');
        $this->execute('DROP TABLE IF EXISTS `stabila_shop_settings`');
        $this->execute('DROP TABLE IF EXISTS `stabila_shop_sales_channel`');
        $this->execute('DROP TABLE IF EXISTS `stabila_shop_notifications`');
    }
}
