<?php
/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 * Primarily responsible for setting up necessary database tables or options.
 *
 * @since      1.0.0
 * @package    FixReport
 * @subpackage FixReport/includes
 * @author     Code Admin LTD (https://www.code-admin.com/)
 */
class FixReport_Activator {

	/**
	 * The code that runs during plugin activation.
	 * Creates the custom database table for storing fix/maintenance logs.
	 *
	 * @since    1.0.0
	 */
	public static function activate() {
		// Make the WordPress database object available
		global $wpdb;

		// Define the custom table name (using the dynamic WordPress prefix)
        // Important: Use $wpdb->prefix to ensure compatibility with all WP installs
		$table_name = $wpdb->prefix . 'fixreport_logs';

		// Get the default database character set and collation
		$charset_collate = $wpdb->get_charset_collate();

		// Include the upgrade.php file for dbDelta() function
        // Necessary for creating or updating database tables
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		// -------------------------------------------------------------
		// Define the SQL table structure for fixreport_logs
		// Note: Must follow dbDelta format strictly (keys and indexes on separate lines)
		// -------------------------------------------------------------
		$sql = "CREATE TABLE $table_name (
			log_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			title VARCHAR(255) NOT NULL DEFAULT '',
			description TEXT NOT NULL,
			type VARCHAR(50) NOT NULL DEFAULT '',         -- e.g., Update, Fix, Error, Maintenance, Security
			priority VARCHAR(20) NOT NULL DEFAULT 'Medium', -- e.g., Low, Medium, High, Critical
			status VARCHAR(20) NOT NULL DEFAULT 'Resolved', -- e.g., Open, In Progress, Resolved, Closed
			user_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,  -- ID of the user who logged the entry
			created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
			updated_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
			PRIMARY KEY  (log_id),
            KEY type (type),                     -- Index for faster searching by type
            KEY priority (priority),             -- Index for faster searching by priority
            KEY status (status),                 -- Index for faster searching by status
            KEY user_id (user_id)                -- Index for faster searching by user
		) $charset_collate;";
        // -------------------------------------------------------------

		// Execute the query using dbDelta()
        // dbDelta() creates the table if it doesn't exist, or updates it if the structure differs
		dbDelta( $sql );

        // You could add code here to check if the table creation was successful,
        // or add an option to track the database schema version for future updates.
        // Example: add_option( 'fixreport_db_version', '1.0' );

		// Log a message (optional, can be removed after confirming it works)
		// error_log('FixReport Database table creation attempted.');

	} // End activate() method

} // End FixReport_Activator class