<?php
/**
 * ZUTALW Activator
 * Fired during plugin activation.
 * Centralized database table creations including Pro Multi-Campaign and Anti-Fraud logic.
 * UPDATED: Added pool_level column to segments table.
 */

if ( ! defined( 'ABSPATH' ) ) { exit; }

class ZUTALW_Activator {

    /**
     * Create or update custom database tables.
     */
    public static function activate() {
        global $wpdb;

        // Required library for dbDelta function
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

        $charset_collate = $wpdb->get_charset_collate();

        /* =========================================================
         * 1. PRO CAMPAIGN TABLES
         * ========================================================= */
        
        // Table: Campaigns (Stores main settings for each wheel campaign)
        $table_campaigns = $wpdb->prefix . 'zutalw_campaigns';
        $sql_campaigns = "CREATE TABLE $table_campaigns (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            name varchar(255) NOT NULL,
            status varchar(20) DEFAULT 'active' NOT NULL,
            settings LONGTEXT DEFAULT NULL,
            start_date datetime NULL,
            end_date datetime NULL,
            tradein_enabled tinyint(1) DEFAULT 1 NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
            PRIMARY KEY  (id)
        ) $charset_collate;";

        // Table: Segments (Dynamic wheel slices for each campaign)
        // FIXED: Added pool_level column
        $table_segments = $wpdb->prefix . 'zutalw_segments';
        $sql_segments = "CREATE TABLE $table_segments (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            campaign_id bigint(20) unsigned NOT NULL,
            label varchar(255) NOT NULL,
            bg_color varchar(20) DEFAULT '#ffffff' NOT NULL,
            text_color varchar(20) DEFAULT '#000000' NOT NULL,
            weight int(11) DEFAULT 1 NOT NULL,
            quantity_total int(11) DEFAULT -1 NOT NULL,
            quantity_remaining int(11) DEFAULT -1 NOT NULL,
            pool_type varchar(20) DEFAULT 'basic' NOT NULL,
            pool_level int(11) DEFAULT 1 NOT NULL, 
            image_url varchar(255) DEFAULT '' NOT NULL,
            sort_order int(11) DEFAULT 0 NOT NULL,
            PRIMARY KEY  (id),
            KEY campaign_id (campaign_id)
        ) $charset_collate;";

        /* =========================================================
         * 2. NEW ANTI-FRAUD TABLES
         * ========================================================= */

        // Table: Spin Limits (Tracks device spins strictly for anti-fraud)
        $table_spin_limits = $wpdb->prefix . 'zutalw_spin_limits';
        $sql_spin_limits = "CREATE TABLE $table_spin_limits (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            campaign_id bigint(20) unsigned NOT NULL,
            device_id varchar(100) NOT NULL,
            spins_total int(11) DEFAULT 1 NOT NULL,
            spins_used int(11) DEFAULT 0 NOT NULL,
            spins_left int(11) DEFAULT 0 NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
            updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
            PRIMARY KEY  (id),
            KEY campaign_device (campaign_id, device_id)
        ) $charset_collate;";

        // Table: Prize Logs (Collects leads and claimed prizes)
        $table_prize_logs = $wpdb->prefix . 'zutalw_prize_logs';
        $sql_prize_logs = "CREATE TABLE $table_prize_logs (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            campaign_id bigint(20) unsigned NOT NULL,
            device_id varchar(100) NOT NULL,
            prize_id bigint(20) unsigned NOT NULL,
            prize_name varchar(255) NOT NULL,
            page_url varchar(255) DEFAULT '' NOT NULL,
            fullname varchar(255) DEFAULT '' NOT NULL,
            phone varchar(50) DEFAULT '' NOT NULL,
            email varchar(100) DEFAULT '' NOT NULL,
            is_trade_in tinyint(1) DEFAULT 0 NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
            PRIMARY KEY  (id),
            KEY campaign_id (campaign_id)
        ) $charset_collate;";
		
		$table_configgame = $wpdb->prefix . 'zutalw_configgame';
       
        $sql_configgame = "CREATE TABLE {$table_configgame} (
            idconfiggame mediumint(9) NOT NULL AUTO_INCREMENT,
            nameconfig varchar(255) DEFAULT NULL,
            dataconfig LONGTEXT DEFAULT NULL,
            `level` mediumint(9) NULL,
            license mediumint(9) DEFAULT 0,
            created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY  (idconfiggame)
        ) {$charset_collate};";
		
        /* =========================================================
         * EXECUTE QUERIES
         * ========================================================= */
        dbDelta( $sql_campaigns );
        dbDelta( $sql_segments );
        dbDelta( $sql_spin_limits );
        dbDelta( $sql_prize_logs );
        dbDelta( $sql_configgame );
        update_option( 'zutalw_db_version', ZUTALW_VERSION );
    }
}