<?php
/**
 * Admin Chat Box Datatable
 *
 * This class is used to builds all of the tables
 *
 * @package ACB\Inc
 */

namespace ACB\Inc;

defined('ABSPATH') || die('Hey, what are you doing here? You silly human!');
/**
 * ACB_DbTables Class Create Chat box database table
 *
 * @since 1.0.0
 */
final class ACB_DbTables {
	/**
	 * Holds the instance of the option table.
	 *
	 * @since 1.0.0
	 * @var ACB
	 */
	private $sql;
	/**
	 * Construct method.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {		
		add_action('init', [$this, 'create_tables']);
	}

	/**
	 * Create database tables
	 *
	 * @since 1.0.0
	 */
	public function create_tables() {
		global $wpdb;
		
		$charset_collate = $wpdb->get_charset_collate();
		
		// Messages table
		$table_messages = $wpdb->prefix . 'acb_messages';
		$sql_messages = "CREATE TABLE $table_messages (
			id mediumint(9) NOT NULL AUTO_INCREMENT,
			user_id varchar(255) NOT NULL,
			user_name varchar(255) NOT NULL,
			user_email varchar(255) NOT NULL,
			user_avatar varchar(500) DEFAULT '',
			message text NOT NULL,
			message_type varchar(50) DEFAULT 'text',
			file_url varchar(500) DEFAULT '',
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			KEY user_id (user_id),
			KEY created_at (created_at)
		) $charset_collate;";
		
		// Users table for session management
		$table_users = $wpdb->prefix . 'acb_users';
		$sql_users = "CREATE TABLE $table_users (
			id mediumint(9) NOT NULL AUTO_INCREMENT,
			firebase_uid varchar(255) NOT NULL,
			user_name varchar(255) NOT NULL,
			user_email varchar(255) NOT NULL,
			user_avatar varchar(500) DEFAULT '',
			auth_provider varchar(50) DEFAULT 'email',
			is_admin tinyint(1) DEFAULT 0,
			last_seen datetime DEFAULT CURRENT_TIMESTAMP,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			UNIQUE KEY firebase_uid (firebase_uid),
			KEY user_email (user_email)
		) $charset_collate;";
		
		// Settings table
		$table_settings = $wpdb->prefix . 'acb_settings';
		$sql_settings = "CREATE TABLE $table_settings (
			id mediumint(9) NOT NULL AUTO_INCREMENT,
			setting_key varchar(255) NOT NULL,
			setting_value longtext NOT NULL,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			UNIQUE KEY setting_key (setting_key)
		) $charset_collate;";
		
		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql_messages);
		dbDelta($sql_users);
		dbDelta($sql_settings);
		
		// Insert default settings
		$this->insert_default_settings();
	}
	
	/**
	 * Insert default settings
	 *
	 * @since 1.0.0
	 */
	private function insert_default_settings() {
		global $wpdb;
		$table_settings = $wpdb->prefix . 'acb_settings';
		
		// Settings already exist, don't insert again
		$existing_settings = $wpdb->get_var("SELECT COUNT(*) FROM $table_settings");
		if ($existing_settings > 0) {
			return; 
		}
		
		$default_settings = [
			'chat_enabled' => '1',
			'frontend_enabled' => '1',
			'admin_enabled' => '1',
			'position' => 'right',
			'primary_color' => '#5A5EB9',
			'welcome_message' => 'Hello! How can I help you today?',
			'max_message_length' => '1000',
			'file_upload_enabled' => '1',
			'max_file_size' => '5242880', // 5MB
			'allowed_file_types' => 'jpg,jpeg,png,gif,pdf,doc,docx',
			// Frontend Widget Settings
			'frontend_widget_enabled' => '1',
			'widget_position' => 'bottom-right',
			'widget_title' => 'Chat Support',
			'widget_description' => 'Need help? Chat with our team!',
			'widget_primary_color' => '#5A5EB9',
			'widget_show_on_pages' => 'all',
			'widget_exclude_pages' => ''
		];
		
		foreach ($default_settings as $key => $value) {
			$wpdb->insert(
				$table_settings,
				[
					'setting_key' => $key,
					'setting_value' => $value
				],
				['%s', '%s']
			);
		}
	}
}
