<?php
/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @package    Cloudi7_ID_Card_Generator
 * @subpackage Cloudi7_ID_Card_Generator/includes
 */

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

/**
 * Class Cloudi7_ICG_Activator
 *
 * Handles tasks that run once when the plugin is activated,
 * such as setting default options and flushing rewrite rules.
 */
class Cloudi7_ICG_Activator {

	/**
	 * Run activation routines.
	 *
	 * Called by register_activation_hook in the main plugin file.
	 * Add any one-time setup logic here (e.g., creating DB tables,
	 * setting default options).
	 *
	 * @return void
	 */
	public static function activate() {
		// Set a default option so we know the plugin has been activated.
		if ( false === get_option( 'cloudi7_icg_version' ) ) {
			add_option( 'cloudi7_icg_version', Cloudi7_ICG_VERSION );
		}

		// Create the database table.
		self::create_tables();

		// Flush rewrite rules so any registered CPTs / taxonomies work immediately.
		flush_rewrite_rules();
	}

	/**
	 * Create the database tables for the plugin.
	 *
	 * Uses dbDelta to create or update the table schema.
	 *
	 * @return void
	 */
	private static function create_tables() {
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		// Cards Table
		$table_cards = $wpdb->prefix . 'cloudi7_icg_cards';
		$sql_cards = "CREATE TABLE $table_cards (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			id_number varchar(20) NOT NULL,
			name varchar(255) NOT NULL,
			photo varchar(255) DEFAULT '',
			organization_type varchar(50) DEFAULT '',
			department varchar(100) DEFAULT '',
			designation varchar(100) DEFAULT '',
			class varchar(50) DEFAULT '',
			email varchar(100) DEFAULT '',
			phone varchar(20) DEFAULT '',
			address text DEFAULT '',
			dob date DEFAULT NULL,
			gov_id varchar(50) DEFAULT '',
			parent_name varchar(255) DEFAULT '',
			parent_phone varchar(20) DEFAULT '',
			blood_group varchar(10) DEFAULT '',
			extra_data longtext DEFAULT '',
			issue_date date DEFAULT NULL,
			expiry_date date DEFAULT NULL,
			created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
			PRIMARY KEY  (id),
			KEY id_number (id_number),
			KEY created_at (created_at)
		) $charset_collate;";

		dbDelta( $sql_cards );

		// Applications Table
		$table_apps = $wpdb->prefix . 'cloudi7_icg_applications';
		$sql_apps = "CREATE TABLE $table_apps (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			email varchar(100) NOT NULL,
			phone varchar(20) DEFAULT '',
			photo varchar(255) DEFAULT '',
			org_type varchar(50) DEFAULT '',
			org_details longtext DEFAULT '',
			dob date DEFAULT NULL,
			gov_id varchar(50) DEFAULT '',
			parent_name varchar(255) DEFAULT '',
			parent_phone varchar(20) DEFAULT '',
			blood_group varchar(10) DEFAULT '',
			status varchar(20) DEFAULT 'pending',
			admin_notes text DEFAULT '',
			created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
			updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
			PRIMARY KEY  (id),
			KEY status (status),
			KEY created_at (created_at)
		) $charset_collate;";

		dbDelta( $sql_apps );
	}
}
