<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * MINIORANGE Admin Apps
 *
 * @package    Admin\Apps
 */

/**
 * Include default apps
 */
require_once 'defaultapps.php';

/**
 * Premium-to-free migration for `mo_oauth_apps_list`.
 *
 * Ensures the stored option is a single array-shaped app entry, which is the
 * free-version format. Premium installs may store multiple entries or object-
 * shaped entries; both are reduced to at most one valid array entry.
 *
 * @return void
 */
function mocognito_migrate_oauth_apps_list_premium_to_free() {
	if ( get_option( 'mocognito_migration_apps_list_v1_done' ) ) {
		return;
	}

	$appslist = get_option( 'mo_oauth_apps_list', false );
	if ( false === $appslist || '' === $appslist ) {
		update_option( 'mocognito_migration_apps_list_v1_done', true );
		return;
	}
	if ( ! is_array( $appslist ) || empty( $appslist ) ) {
		delete_option( 'mo_oauth_apps_list' );
		update_option( 'mocognito_migration_apps_list_v1_done', true );
		return;
	}

	// Keep only array-shaped entries (free format); drop legacy object entries.
	$valid_apps = array_filter( $appslist, fn( $app ) => is_array( $app ) );

	// Free version supports at most 1 app.
	if ( count( $valid_apps ) > 1 ) {
		$valid_apps = array_slice( array_values( $valid_apps ), 0, 1 );
	}

	if ( count( $valid_apps ) !== count( $appslist ) ) {
		if ( empty( $valid_apps ) ) {
			delete_option( 'mo_oauth_apps_list' );
		} else {
			update_option( 'mo_oauth_apps_list', $valid_apps );
		}
		add_action(
			'admin_notices',
			function () {
				echo '<div class="notice notice-warning is-dismissible"><p>'
					. esc_html__( 'miniOrange OAuth: your app configuration was updated during a premium-to-free migration. Some entries were removed because they are not supported in the free version.', 'login-with-cognito' )
					. '</p></div>';
			}
		);
	}

	update_option( 'mocognito_migration_apps_list_v1_done', true );
}

/**
 * App list page
 */
function mocognito_applist_page() {

	if ( ! current_user_can( "manage_options" ) ) {
        return;
    }
	
	mocognito_migrate_oauth_apps_list_premium_to_free();
	?>
	<style>
		.tableborder {
			border-collapse: collapse;
			width: 100%;
			border-color:#eee;
		}

		.tableborder th, .tableborder td {
			text-align: left;
			padding: 8px;
			border-color:#eee;
		}

		.tableborder tr:nth-child(even){background-color: #f2f2f2}
	</style>
	<div id="mo_oauth_app_list" class="mo_table_layout">
	<?php

	if ( isset( $_GET['action'] ) && 'delete' === $_GET['action'] ) {
		if ( isset( $_GET['app'] ) && check_admin_referer( 'delete_' . sanitize_text_field( wp_unslash( $_GET['app'] ) ) ) ) {
			mocognito_delete_app( sanitize_text_field( wp_unslash( $_GET['app'] ) ) );
			if ( 'CognitoApp' === $_GET['app'] ) {
				?>
					<script>
						let url = window.location.href;
						url = url.split("&action=delete&app=CognitoApp")[0];
						window.location.replace(url);
					</script>
				<?php
				exit();
			}
		}
	} elseif ( isset( $_GET['action'] ) && 'instructions' === $_GET['action'] ) {
		if ( isset( $_GET['appId'] ) ) {
			MoCognito_Admin_Admin_Guides::instructions( sanitize_text_field( wp_unslash( $_GET['appId'] ) ) );
		}
	}

	if ( isset( $_GET['action'] ) && 'add' === $_GET['action'] ) {
		MoCognito_Admin_Apps::add_app();
	} elseif ( isset( $_GET['action'] ) && 'update' === $_GET['action'] ) {
		if ( isset( $_GET['app'] ) ) {
			MoCognito_Admin_Apps::update_app( sanitize_text_field( wp_unslash( $_GET['app'] ) ) );
		}
	} elseif ( get_option( 'mo_oauth_apps_list' ) ) {
		$appslist = get_option( 'mo_oauth_apps_list' );
		if ( count( $appslist ) > 0 ) {
			echo "<br><a href='#'><button disabled style='float:right'>Add Application</button></a>";
		} else {
			echo "<br><a href='admin.php?page=mo_oauth_settings&action=add'><button style='float:right'>Add Application</button></a>";
		}
		echo '<h3>Applications List</h3>';
		if ( is_array( $appslist ) && count( $appslist ) > 0 ) {
			echo "<p style='color:#a94442;background-color:#f2dede;border-color:#ebccd1;border-radius:5px;padding:12px'>You can only add 1 application with free version. Upgrade to <a href='admin.php?page=mo_oauth_settings&tab=licensing'><b>enterprise</b></a> to add more.</p>";
		}
		echo "<table class='tableborder'>";
		echo '<tr><th><b>Name</b></th><th>Action</th></tr>';
		foreach ( $appslist as $key => $app ) {
			$currentapp     = $app;
			$delete_app_url = wp_nonce_url( 'admin.php?page=mo_oauth_settings&tab=config&action=delete&app=' . esc_attr( $key ), 'delete_' . esc_attr( $key ) );
			echo '<tr><td>' . esc_attr( $key ), ' (', esc_attr( $currentapp['apptype'] ), ') ' . "</td><td><a href='admin.php?page=mo_oauth_settings&tab=config&action=update&app=" . esc_attr( $key ) . "'>Edit Application</a> | <a href='admin.php?page=mo_oauth_settings&tab=attributemapping&app=" . esc_attr( $key ) . "#attribute-mapping'>Attribute Mapping</a> | <a href='admin.php?page=mo_oauth_settings&tab=attributemapping&app=" . esc_attr( $key ) . "#role-mapping'>Role Mapping</a> | <a onclick='return confirm(\"Are you sure you want to delete this item?\")' href='" . esc_url( $delete_app_url ) . "'>Delete</a> | ";
			if ( isset( $_GET['action'] ) ) {
				if ( 'instructions' === $_GET['action'] ) {
					echo "<a href='admin.php?page=mo_oauth_settings&tab=config'>Hide Instructions</a></td></tr>";
				}
			} else {
				$instructions_app_url = wp_nonce_url( 'admin.php?page=mo_oauth_settings&tab=config&action=instructions&appId=' . ( isset( $app['appId'] ) ? esc_attr( $app['appId'] ) : '' ), 'instructions_' . ( isset( $app['appId'] ) ? esc_attr( $app['appId'] ) : '' ) );
				echo "<a href='" . esc_url( $instructions_app_url ) . "'>How to Configure?</a></td></tr>";
			}
		}
		echo '</table>';
		echo '<br><br>';

	} else {
		MoCognito_Admin_Apps::add_app();
	}
	?>
		</div>
	<?php
}

/**
 * Delete app
 *
 * @param AppName $appname application name.
 */
function mocognito_delete_app( $appname ) {
	$appslist = get_option( 'mo_oauth_apps_list' );
	if ( ! is_array( $appslist ) || empty( $appslist ) ) {
		return;
	}
	foreach ( $appslist as $key => $app ) {
		if ( $appname === $key ) {
			if ( 'wso2' === $appslist[ $appname ]['appId'] ) {
				delete_option( 'mo_oauth_client_custom_token_endpoint_no_csecret' );
			}
			unset( $appslist[ $key ] );
			delete_option( 'mo_oauth_client_disable_authorization_header' );
			delete_option( 'mo_oauth_attr_name_list' );
			delete_option( 'mo_oauth_apps_list' );
			$notices = get_option( 'mo_oauth_client_notice_messages' );
			if ( isset( $notices['attr_mapp_notice'] ) ) {
				unset( $notices['attr_mapp_notice'] );
				update_option( 'mo_oauth_client_notice_messages', $notices );
			}
		}
	}
	update_option( 'mo_oauth_apps_list', $appslist );
	?>
		<script>
			window.location.href = "<?php echo esc_url( admin_url( 'admin.php?page=mo_oauth_settings&tab=config' ) ); ?>";
		</script>
		<?php
}
