<?php
/**
 * Installation class for database setup and cleanup
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class PC_Announcements_274_Install {
    
    /**
     * Install plugin - create database tables and set default options
     */
    public static function install() {
        global $wpdb;
        
        // Ensure plugin constants are defined
        if (!defined('PC_ANNOUNCEMENTS_274_VERSION')) {
            define('PC_ANNOUNCEMENTS_274_VERSION', '1.0.0');
        }
        
        if (!function_exists('dbDelta')) {
            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        }
        
        $table_name = $wpdb->prefix . 'pc_announcements_274';
        
        $charset_collate = '';
        if (method_exists($wpdb, 'get_charset_collate')) {
            $charset_collate = $wpdb->get_charset_collate();
        }
        
        if (empty($charset_collate)) {
            $charset_collate = 'DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci';
        }
        
        $sql = "CREATE TABLE $table_name (
            id int(11) NOT NULL AUTO_INCREMENT,
            title varchar(255) NOT NULL,
            message text NOT NULL,
            image_url varchar(500) DEFAULT NULL,
            banner_color varchar(7) DEFAULT '#0d47a1',
            link_url varchar(500) DEFAULT NULL,
            start_date datetime DEFAULT NULL,
            end_date datetime DEFAULT NULL,
            status varchar(20) DEFAULT 'active',
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            created_by int(11) NOT NULL,
            PRIMARY KEY  (id),
            KEY status (status),
            KEY start_date (start_date),
            KEY end_date (end_date)
        ) $charset_collate;";
        
        dbDelta($sql);
        
        add_option('pc_announcements_274_version', PC_ANNOUNCEMENTS_274_VERSION);
        add_option('pc_announcements_274_db_version', '1.0');
    }
    
    /**
     * Get table name
     */
    public static function get_table_name() {
        global $wpdb;
        if (!isset($wpdb)) {
            return null;
        }
        return $wpdb->prefix . 'pc_announcements_274';
    }
}