<?php
defined( 'ABSPATH' ) || exit;

class NASEO_Activator {

    public static function activate(): void {
        self::create_table();
        wp_schedule_event( strtotime( 'today 09:00:00' ), 'daily', 'naseo_publish_cron' );
        wp_schedule_event( time(), 'weekly', 'naseo_license_check' );
    }

    public static function deactivate(): void {
        wp_clear_scheduled_hook( 'naseo_publish_cron' );
        wp_clear_scheduled_hook( 'naseo_license_check' );
    }

    private static function create_table(): void {
        global $wpdb;
        $table   = $wpdb->prefix . 'naseo_queue';
        $charset = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE IF NOT EXISTS {$table} (
            id             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            title          VARCHAR(500)    NOT NULL,
            keyword        VARCHAR(255)    NOT NULL DEFAULT '',
            lsi_keywords   TEXT,
            category       VARCHAR(255)    DEFAULT '',
            scheduled_date DATE            DEFAULT NULL,
            schedule       VARCHAR(50)     DEFAULT 'daily',
            status         VARCHAR(20)     NOT NULL DEFAULT 'pending',
            post_id        BIGINT UNSIGNED DEFAULT NULL,
            error_msg      TEXT,
            created_at     DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at     DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY status (status),
            KEY scheduled_date (scheduled_date)
        ) {$charset};";

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