<?php

namespace SoftMultimediaPlayer;

defined( 'ABSPATH' ) || exit;

class Install {

	/**
	 * Activation hook for the plugin.
	 *
	 * This function is called when the plugin is activated. It creates the
	 * necessary database tables and inserts the default data into them.
	 */
	public static function activate() {
		self::create_tables();
		self::create_default_data();
		self::flush_rewrite_rules();
	}


	public static function flush_rewrite_rules() {
		add_rewrite_rule( '^player/([0-9]+)/?$', 'index.php?multimedia-player=$matches[1]', 'top' );
		flush_rewrite_rules();
	}

	/**
	 * Create the database tables for the plugin.
	 *
	 * This function creates two tables in the database: one for storing the
	 * player configuration and another for storing the player statistics.
	 *
	 * @since 1.0.0
	 */
	private static function create_tables() {

		global $wpdb;
		$wpdb->hide_errors();
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

		$tables = [

			// Players Table
			"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}smp_players(
         	id bigint(20) NOT NULL AUTO_INCREMENT,
			status tinyint(1) NOT NULL DEFAULT 1, 
			config longtext NULL,
			title varchar(255) NULL,
			type varchar(255) NULL,
			locations longtext NULL,
			created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
			updated_at DATETIME NOT NULL,
			PRIMARY KEY  (id)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",

			// Statistics table
			"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}smp_statistics(
         	    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
		        player_id VARCHAR(100) NOT NULL,
                player_type VARCHAR(100) NOT NULL,
		        track INT NOT NULL,
		        action VARCHAR(50) NOT NULL,
		        browser VARCHAR(100) DEFAULT '',
		        os VARCHAR(100) DEFAULT '',
		        device VARCHAR(100) DEFAULT '',
		        user_id INT NULL,
		        ip VARCHAR(100) DEFAULT '',
		        country VARCHAR(100) DEFAULT '',
		        page TEXT,
		        duration INT DEFAULT 0,
		        timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
		        PRIMARY KEY (id),
		        INDEX track_idx (track),
		        INDEX action_idx (action),
		        INDEX timestamp_idx (timestamp)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
		];

		foreach ( $tables as $table ) {
			dbDelta( $table );
		}
	}

	/**
	 * Create plugin settings default data
	 *
	 * @since 1.0.0
	 */
	private static function create_default_data() {

		$version      = get_option( 'smp_version', '0' );
		$db_version   = get_option( 'smp_db_version', '0' );
		$install_time = get_option( 'smp_install_time', '' );

		if ( empty( $version ) ) {
			update_option( 'smp_version', SOFT_MULTIMEDIA_PLAYER_VERSION );
		}

		if ( empty( $db_version ) ) {
			update_option( 'smp_db_version', SOFT_MULTIMEDIA_PLAYER_DB_VERSION );
		}

		if ( empty( $install_time ) ) {
			$date_format = get_option( 'date_format' );
			$time_format = get_option( 'time_format' );

			update_option( 'smp_install_time', gmdate( $date_format . ' ' . $time_format ) );
		}
	}
}