<?php

/**
 * Database schema versioning for KAZCODE Universal Storage v2 tables.
 *
 * @package Kazcode\WpStorage
 */
declare (strict_types=1);
namespace Kazcode\WpStorage\Infrastructure;

defined('ABSPATH') || exit;
/**
 * Creates/upgrades custom tables. Must stay fast on activate / plugins_loaded.
 */
final class SchemaMigrator
{
    public const OPTION_KEY = 's3ms_schema_version';
    public const VERSION = 1;
    /**
     * Run pending schema upgrades.
     */
    public function maybe_upgrade(): void
    {
        $current = (int) get_option(self::OPTION_KEY, 0);
        if ($current >= self::VERSION) {
            return;
        }
        $this->install_v1();
        update_option(self::OPTION_KEY, self::VERSION, \false);
    }
    /**
     * Create profiles + objects tables (dbDelta).
     */
    private function install_v1(): void
    {
        global $wpdb;
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        $charset = $wpdb->get_charset_collate();
        $profiles = $wpdb->prefix . 's3ms_storage_profiles';
        $objects = $wpdb->prefix . 's3ms_objects';
        $sql_profiles = "CREATE TABLE {$profiles} (\n\t\t\tid bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n\t\t\tuuid char(36) NOT NULL,\n\t\t\tname varchar(191) NOT NULL,\n\t\t\tprovider_type varchar(32) NOT NULL,\n\t\t\tbucket varchar(255) NOT NULL DEFAULT '',\n\t\t\tregion varchar(64) NOT NULL DEFAULT '',\n\t\t\tendpoint varchar(255) NOT NULL DEFAULT '',\n\t\t\tpath_style tinyint(1) NOT NULL DEFAULT 0,\n\t\t\tprefix varchar(255) NOT NULL DEFAULT '',\n\t\t\tdelivery_type varchar(16) NOT NULL DEFAULT 'storage',\n\t\t\tdelivery_base_url text NULL,\n\t\t\tcdn_includes_prefix tinyint(1) NOT NULL DEFAULT 0,\n\t\t\tcredential_mode varchar(16) NOT NULL DEFAULT 'keys',\n\t\t\tcredentials_ref varchar(64) NOT NULL DEFAULT '',\n\t\t\tis_default_upload_target tinyint(1) NOT NULL DEFAULT 0,\n\t\t\tis_read_only tinyint(1) NOT NULL DEFAULT 0,\n\t\t\tlocation_locked tinyint(1) NOT NULL DEFAULT 0,\n\t\t\tcreated_at datetime NOT NULL,\n\t\t\tupdated_at datetime NOT NULL,\n\t\t\tPRIMARY KEY  (id),\n\t\t\tUNIQUE KEY uuid (uuid),\n\t\t\tKEY provider_type (provider_type),\n\t\t\tKEY is_default_upload_target (is_default_upload_target)\n\t\t) {$charset};";
        $sql_objects = "CREATE TABLE {$objects} (\n\t\t\tid bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n\t\t\tattachment_id bigint(20) unsigned NOT NULL,\n\t\t\tstorage_profile_id bigint(20) unsigned NOT NULL,\n\t\t\tlocal_relative_path varchar(512) NOT NULL,\n\t\t\tobject_key varchar(1024) NOT NULL,\n\t\t\tvariant_type varchar(32) NOT NULL DEFAULT 'size',\n\t\t\tmime_type varchar(100) NOT NULL DEFAULT '',\n\t\t\tsize_bytes bigint(20) unsigned DEFAULT NULL,\n\t\t\tetag varchar(128) DEFAULT NULL,\n\t\t\tchecksum varchar(128) DEFAULT NULL,\n\t\t\tlocal_status varchar(16) NOT NULL DEFAULT 'unknown',\n\t\t\tremote_status varchar(16) NOT NULL DEFAULT 'pending',\n\t\t\tattempt_count int(10) unsigned NOT NULL DEFAULT 0,\n\t\t\tlast_error_code varchar(64) DEFAULT NULL,\n\t\t\tlast_error_message text,\n\t\t\toffloaded_at datetime DEFAULT NULL,\n\t\t\tverified_at datetime DEFAULT NULL,\n\t\t\tcreated_at datetime NOT NULL,\n\t\t\tupdated_at datetime NOT NULL,\n\t\t\tPRIMARY KEY  (id),\n\t\t\tUNIQUE KEY profile_object_key (storage_profile_id, object_key(191)),\n\t\t\tKEY attachment_id (attachment_id),\n\t\t\tKEY remote_status (remote_status),\n\t\t\tKEY updated_at (updated_at),\n\t\t\tKEY attachment_remote (attachment_id, remote_status),\n\t\t\tKEY profile_attachment (storage_profile_id, attachment_id)\n\t\t) {$charset};";
        dbDelta($sql_profiles);
        dbDelta($sql_objects);
    }
}
