<?php
if (!defined('ABSPATH')) {
	exit;
}
/* @var UpVimeo_Api_Helper $upvimeo_vimeo_helper */

$upvimeo_vimeo_helper = isset($args['upvimeo_helper']) ? $args['upvimeo_helper'] : (isset($upvimeo_vimeo_helper) ? $upvimeo_vimeo_helper : null);
$upvimeo_db_helper = isset($args['db_helper']) ? $args['db_helper'] : (isset($upvimeo_db_helper) ? $upvimeo_db_helper : null);
$upvimeo_settings_helper = isset($args['settings_helper']) ? $args['settings_helper'] : (isset($upvimeo_settings_helper) ? $upvimeo_settings_helper : null);

/* @var UpVimeo_Db_Helper $upvimeo_db_helper */
/* @var UpVimeo_Settings_Helper $upvimeo_settings_helper */

?>
<div class="wrap upvimeo-settings-page upvimeo-library-page">
	<?php
	// Add capability check at the very top.
	if (!current_user_can('upload_files')) {
		wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'upvimeo-direct-uploads'));
	}

	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$upvimeo_action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : '';
	$upvimeo_id = isset($_GET['id']) ? sanitize_text_field(wp_unslash($_GET['id'])) : '';

	if ( ( $upvimeo_action === 'edit' || $upvimeo_action === 'delete' ) && ! empty( $upvimeo_id ) ) {
		if ( ! isset( $_REQUEST['upvimeo_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['upvimeo_nonce'] ) ), 'upvimeo_action' ) ) {
			wp_die( esc_html__( "Permission denied. Security check failed.", 'upvimeo-direct-uploads' ), esc_html__( 'Error', 'upvimeo-direct-uploads' ) );
		}
	}

	if (!upvimeo_php_version_ok()) {
		include 'outdated.php';
	} else {
		if ($upvimeo_action === 'new') {
			if ($upvimeo_vimeo_helper->is_connected) {
				if ($upvimeo_vimeo_helper->can_upload()) {
					include 'library-upload.php';
				} else {
					include 'not-allowed-upload.php';
				}
			} else {
				include 'not-connected.php';
			}
		} elseif ($upvimeo_action === 'edit' && !empty($upvimeo_id)) {
			include 'library-edit.php';
		} elseif (empty($upvimeo_action) || 'delete' === $upvimeo_action || '-1' === $upvimeo_action) {
			include 'library-list.php';
		} else {
			esc_html_e('Invalid action', 'upvimeo-direct-uploads');
		}
	}
	?>
</div>