<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/migrations/038_product_views.php
// Description: Migration 038 -- create wp_amai_product_views table for re-engagement tracking - NUCLEAR+++
// Last Edited: 2026-05-16
// ==================================================

namespace AutoMarketerAI\Migrations;

class Migration_038_ProductViews
{
    public function up(): void
    {
        global $wpdb;

        $table   = $wpdb->prefix . 'amai_product_views';
        $charset = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE IF NOT EXISTS `{$table}` (
            `id`              BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            `session_key`     VARCHAR(64)     NOT NULL,
            `product_id`      BIGINT UNSIGNED NOT NULL,
            `email`           VARCHAR(200)    NULL DEFAULT NULL,
            `first_name`      VARCHAR(100)    NULL DEFAULT NULL,
            `viewed_at`       DATETIME        NOT NULL,
            `purchased`       TINYINT(1)      NOT NULL DEFAULT 0,
            `followup_sent_at` DATETIME       NULL DEFAULT NULL,
            `created_at`      DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (`id`),
            INDEX `idx_session_key` (`session_key`),
            INDEX `idx_email`       (`email`),
            INDEX `idx_viewed_at`   (`viewed_at`)
        ) {$charset};";

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