<?php

use EasyLinkAutomations\Config;
use EasyLinkAutomations\Database\Connection as DB;
use EasyLinkAutomations\Migration\Migration;

if (!defined('ABSPATH')) {
    exit;
}

final class EasyLinkAutomationsPluginOptions extends Migration
{
    public function up(): void
    {
        Config::updateOption('db_version', Config::DB_VERSION, true);
        Config::updateOption('installed', time(), true);
        Config::updateOption('version', Config::VERSION, true);
        Config::updateOption('settings', [], true);
    }

    public function down(): void
    {
        $options = [
            Config::withPrefix('db_version'),
            Config::withPrefix('installed'),
            Config::withPrefix('version'),
            Config::withPrefix('settings'),
        ];

        $placeholders = implode(',', array_fill(0, count($options), '%s'));

        DB::query(
            DB::prepare(
                "DELETE FROM `" . DB::wpPrefix() . "options` WHERE option_name IN ({$placeholders})",
                $options
            )
        );
    }
}
