<?php

declare (strict_types=1);
namespace Slash\Booking\Persistence;

use Slash\Booking\Plugin;
use wpdb;
final class Migrator
{
    public function __construct(private readonly wpdb $wpdb)
    {
    }
    public function migrate(): void
    {
        $currentVersion = (int) get_option('sb_db_version', 0);
        if ($currentVersion >= Plugin::DB_VERSION) {
            return;
        }
        require_once \ABSPATH . 'wp-admin/includes/upgrade.php';
        $charset = $this->wpdb->get_charset_collate();
        $prefix = $this->wpdb->prefix;
        $statements = ["CREATE TABLE {$prefix}sb_services (\n                id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n                slug VARCHAR(64) NOT NULL,\n                name VARCHAR(160) NOT NULL,\n                duration_min SMALLINT UNSIGNED NOT NULL,\n                buffer_before_min SMALLINT UNSIGNED NOT NULL DEFAULT 0,\n                buffer_after_min SMALLINT UNSIGNED NOT NULL DEFAULT 0,\n                min_lead_time_hours SMALLINT UNSIGNED NOT NULL DEFAULT 24,\n                max_horizon_days SMALLINT UNSIGNED NOT NULL DEFAULT 60,\n                color VARCHAR(7) NOT NULL DEFAULT '#0ea5e9',\n                active TINYINT(1) NOT NULL DEFAULT 1,\n                sort_order SMALLINT UNSIGNED NOT NULL DEFAULT 0,\n                settings LONGTEXT NULL,\n                created_at DATETIME NOT NULL,\n                updated_at DATETIME NOT NULL,\n                PRIMARY KEY (id),\n                UNIQUE KEY uk_slug (slug)\n            ) {$charset};", "CREATE TABLE {$prefix}sb_bookings (\n                id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n                public_uid CHAR(36) NOT NULL,\n                service_id BIGINT UNSIGNED NOT NULL,\n                status VARCHAR(16) NOT NULL,\n                starts_at_utc DATETIME NOT NULL,\n                ends_at_utc DATETIME NOT NULL,\n                timezone VARCHAR(64) NOT NULL,\n                customer_name VARCHAR(160) NOT NULL,\n                customer_email VARCHAR(200) NOT NULL,\n                customer_phone VARCHAR(40) NOT NULL,\n                customer_address TEXT NULL,\n                customer_meta LONGTEXT NULL,\n                notes TEXT NULL,\n                google_event_id VARCHAR(255) NULL,\n                google_event_etag VARCHAR(255) NULL,\n                decision_token_hash VARCHAR(64) NULL,\n                reminder_sent_at DATETIME NULL,\n                ip VARCHAR(45) NULL,\n                user_agent VARCHAR(255) NULL,\n                created_at DATETIME NOT NULL,\n                updated_at DATETIME NOT NULL,\n                PRIMARY KEY (id),\n                UNIQUE KEY uk_public_uid (public_uid),\n                KEY idx_status_starts (status, starts_at_utc),\n                KEY idx_google_event (google_event_id)\n            ) {$charset};", "CREATE TABLE {$prefix}sb_busy_blocks (\n                id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n                source VARCHAR(16) NOT NULL,\n                source_id VARCHAR(255) NOT NULL,\n                google_account_id BIGINT UNSIGNED NULL,\n                starts_at_utc DATETIME NOT NULL,\n                ends_at_utc DATETIME NOT NULL,\n                summary VARCHAR(255) NULL,\n                last_synced_at DATETIME NOT NULL,\n                PRIMARY KEY (id),\n                UNIQUE KEY uk_source (source, source_id),\n                KEY idx_range (starts_at_utc, ends_at_utc)\n            ) {$charset};", "CREATE TABLE {$prefix}sb_google_accounts (\n                id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n                label VARCHAR(120) NOT NULL,\n                calendar_id VARCHAR(200) NOT NULL,\n                oauth_refresh_token_enc LONGTEXT NULL,\n                oauth_access_token_enc LONGTEXT NULL,\n                oauth_expires_at DATETIME NULL,\n                watch_channel_id VARCHAR(80) NULL,\n                watch_resource_id VARCHAR(255) NULL,\n                watch_token_secret VARCHAR(80) NULL,\n                watch_expires_at DATETIME NULL,\n                sync_token VARCHAR(255) NULL,\n                last_full_sync_at DATETIME NULL,\n                reconnect_required TINYINT(1) NOT NULL DEFAULT 0,\n                created_at DATETIME NOT NULL,\n                updated_at DATETIME NOT NULL,\n                PRIMARY KEY (id)\n            ) {$charset};", "CREATE TABLE {$prefix}sb_sync_log (\n                id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n                ts DATETIME NOT NULL,\n                level VARCHAR(10) NOT NULL,\n                direction VARCHAR(8) NOT NULL,\n                entity VARCHAR(32) NOT NULL,\n                entity_id BIGINT UNSIGNED NULL,\n                google_event_id VARCHAR(255) NULL,\n                action VARCHAR(40) NOT NULL,\n                payload LONGTEXT NULL,\n                status VARCHAR(16) NOT NULL,\n                error_message TEXT NULL,\n                PRIMARY KEY (id),\n                KEY idx_ts (ts),\n                KEY idx_entity (entity, entity_id)\n            ) {$charset};", "CREATE TABLE {$prefix}sb_mail_templates (\n                id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n                event_key VARCHAR(64) NOT NULL,\n                subject VARCHAR(255) NOT NULL,\n                html_body LONGTEXT NOT NULL,\n                text_body LONGTEXT NULL,\n                enabled TINYINT(1) NOT NULL DEFAULT 1,\n                updated_at DATETIME NOT NULL,\n                updated_by BIGINT UNSIGNED NULL,\n                PRIMARY KEY (id),\n                UNIQUE KEY uk_event_key (event_key)\n            ) {$charset};"];
        foreach ($statements as $sql) {
            dbDelta($sql);
        }
        update_option('sb_db_version', Plugin::DB_VERSION, \false);
    }
}
