<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/migrations/036_comment_form.php
// Description: Creates wp_amai_comment_form_rate_limit table and seeds default comment form options - NUCLEAR+++
// Last Edited: 2026-05-15
// ==================================================

namespace AutoMarketerAI\Migrations;

defined('ABSPATH') || exit;

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

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

        // Idempotent -- skip if table already exists.
        $exists = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
                 WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s",
                DB_NAME,
                $table
            )
        );

        if ((int) $exists === 0) {
            require_once ABSPATH . 'wp-admin/includes/upgrade.php';
            $sql = "CREATE TABLE `{$table}` (
                `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
                `ip_hash`      VARCHAR(64)     NOT NULL,
                `post_id`      BIGINT UNSIGNED NOT NULL DEFAULT 0,
                `submitted_at` DATETIME        NOT NULL,
                PRIMARY KEY (`id`),
                KEY `idx_ip_hash_submitted` (`ip_hash`, `submitted_at`)
            ) {$charset};";
            dbDelta($sql);
        }

        // Seed default options -- only if not already set.
        if (get_option('amai_comment_form_posts_enabled') === false) {
            add_option('amai_comment_form_posts_enabled', '0');
        }
        if (get_option('amai_comment_form_products_enabled') === false) {
            add_option('amai_comment_form_products_enabled', '0');
        }
        if (get_option('amai_comment_form_rate_limit') === false) {
            add_option('amai_comment_form_rate_limit', '10');
        }
        if (get_option('amai_comment_form_reply_mode_posts') === false) {
            add_option('amai_comment_form_reply_mode_posts', 'manual');
        }
        if (get_option('amai_comment_form_reply_mode_products') === false) {
            add_option('amai_comment_form_reply_mode_products', 'manual');
        }
        if (get_option('amai_comment_profanity_words') === false) {
            add_option('amai_comment_profanity_words', implode("\n", [
                'spam',
                'scam',
                'idiot',
                'stupid',
                'hate',
                'kill',
                'fuck',
                'shit',
                'ass',
                'bitch',
                'bastard',
                'crap',
                'damn',
                'dick',
                'piss',
                'cunt',
                'whore',
                'slut',
            ]));
        }
    }
}