<?php
if ( ! defined( 'ABSPATH' ) ) exit;
require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/scholar_sync_functions.php';

$action = isset( $_REQUEST['action'] ) ? sanitize_key( wp_unslash( $_REQUEST['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- action is read-only routing; each case verifies its own nonce below
switch ( $action ) {
    case 'add-article':
        // also for update
        if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) ), 'scholar_sync_add_article' ) ) {
            wp_die( esc_html__( 'Security check failed.', 'scholar-sync' ) );
        }
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/add_article.php';
        break;
    case 'delete-article':
        $is_bulk      = isset( $_GET['post'] ) && is_array( $_GET['post'] ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- local routing variable
        $nonce_action = $is_bulk ? 'bulk-articles' : 'scholar_sync_delete_article'; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- local routing variable
        if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) ), $nonce_action ) ) {
            wp_die( esc_html__( 'Security check failed.', 'scholar-sync' ) );
        }
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/delete_article.php';
        break;
    case 'delete-author':
        if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'GET' && scholar_sync_get_param( 'author-id' ) !== '' ) {
            if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) ), 'scholar_sync_delete_author' ) ) {
                wp_die( esc_html__( 'Security check failed.', 'scholar-sync' ) );
            }
            require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/delete_author.php';
            scholar_sync_delete_authors( scholar_sync_get_param( 'author-id' ) );
            wp_safe_redirect( admin_url( 'admin.php?page=scholar-sync-authors-slug' ) );
            exit;
        }
        break;
    case 'update-author':
        if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) ), 'scholar_sync_update_author' ) ) {
            wp_die( esc_html__( 'Security check failed.', 'scholar-sync' ) );
        }
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/update_author.php';
        break;
    case 'add-author':
        if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) ), 'scholar_sync_update_author' ) ) {
            wp_die( esc_html__( 'Security check failed.', 'scholar-sync' ) );
        }
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/add_author_manual.php';
        break;
    case 'update-author-directory-link':
        if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) ), 'scholar_sync_update_directory_link' ) ) {
            wp_die( esc_html__( 'Security check failed.', 'scholar-sync' ) );
        }
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/update_author_directory_link.php';
        break;
    case 'save-group':
        if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) ), 'scholar_sync_save_group' ) ) {
            wp_die( esc_html__( 'Security check failed.', 'scholar-sync' ) );
        }
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/save_group.php';
        break;
    case 'delete-group':
        if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'GET' && scholar_sync_get_param( 'group-id' ) !== '' ) {
            if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) ), 'scholar_sync_delete_group' ) ) {
                wp_die( esc_html__( 'Security check failed.', 'scholar-sync' ) );
            }
            require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/delete_group.php';
            scholar_sync_delete_groups( scholar_sync_get_param( 'group-id' ) );
            wp_safe_redirect( admin_url( 'admin.php?page=scholar-sync-groups-slug' ) );
            exit;
        }
        break;
    case 'save-co-author':
        if ( ! current_user_can( 'edit_others_posts' ) ) {
            wp_die( esc_html__( 'You do not have permission to perform this action.', 'scholar-sync' ) );
        }
        if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) ), 'scholar_sync_save_co_author' ) ) {
            wp_die( esc_html__( 'Security check failed.', 'scholar-sync' ) );
        }
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/save_co_author.php';
        break;
    case 'delete-co-author':
        if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'GET' && scholar_sync_get_param( 'co-author-id' ) !== '' ) {
            if ( ! current_user_can( 'edit_others_posts' ) ) {
                wp_die( esc_html__( 'You do not have permission to perform this action.', 'scholar-sync' ) );
            }
            if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ?? '' ) ), 'scholar_sync_delete_co_author' ) ) {
                wp_die( esc_html__( 'Security check failed.', 'scholar-sync' ) );
            }
            global $wpdb;
            $table = SCHOLAR_SYNC_DB_CO_AUTHORS; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- local routing variable
            $wpdb->delete( $table, array( 'co_author_id' => (int) scholar_sync_get_param( 'co-author-id' ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Deletion bypasses cache and uses direct DB call safely
            wp_safe_redirect( admin_url( 'admin.php?page=scholar-sync-co-authors-slug' ) );
            exit;
        }
        break;
    default:
}
