<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/migrations/044_ga4_metrics.php
// Description: Migration 044 -- create wp_amai_ga4_metrics table - NUCLEAR+++
// Last Edited: 2026-05-18
// ==================================================

namespace AutoMarketerAI\Migrations;

defined('ABSPATH') || exit;

final class Migration_044_Ga4Metrics
{
    public const VERSION     = '044';
    public const DESCRIPTION = 'Create amai_ga4_metrics table';

    public function up(): void
    {
        global $wpdb;
        $charset = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}amai_ga4_metrics` (
            `id`                 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            `post_id`            BIGINT UNSIGNED NOT NULL,
            `ga_page_path`       VARCHAR(1000)   NOT NULL DEFAULT '',
            `sessions_30d`       INT UNSIGNED    NOT NULL DEFAULT 0,
            `sessions_prev_30d`  INT UNSIGNED    NOT NULL DEFAULT 0,
            `pageviews_30d`      INT UNSIGNED    NOT NULL DEFAULT 0,
            `pageviews_prev_30d` INT UNSIGNED    NOT NULL DEFAULT 0,
            `last_synced_at`     DATETIME        NOT NULL,
            PRIMARY KEY (`id`),
            UNIQUE KEY `post_id` (`post_id`),
            KEY `sessions_30d` (`sessions_30d`),
            KEY `last_synced_at` (`last_synced_at`)
        ) {$charset};";

        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta($sql);
    }
}