<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- admin view partial; local variables are not global
// phpcs:disable WordPress.Security.NonceVerification -- read-only admin view; capability check handled by admin menu registration
// phpcs:disable WordPress.Security.ValidatedSanitizedInput -- GET params cast to int or sanitize_key

$action       = sanitize_key( $_GET['action'] ?? 'create' );
$co_author_id = isset( $_GET['co-author-id'] ) ? (int) $_GET['co-author-id'] : 0;
$is_edit      = ( $action === 'edit' && $co_author_id > 0 );

$record = array(
    'co_author_id' => 0,
    'full_name'    => '',
    'short_name'   => '',
    'image_url'    => '',
    'profile_link' => '',
    'affiliations' => '',
);

if ( $is_edit ) {
    global $wpdb;
    $table = SCHOLAR_SYNC_DB_CO_AUTHORS;
    $row   = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- %i is the WordPress identifier placeholder (WP 6.2+)
            'SELECT * FROM %i WHERE co_author_id = %d',
            $table,
            $co_author_id
        ), ARRAY_A
    );
    if ( $row ) {
        $record = $row;
    }
}

$form_title  = $is_edit ? __( 'Edit Co-Author', 'scholar-sync' ) : __( 'Add New Co-Author', 'scholar-sync' );
$list_url    = add_query_arg( array( 'page' => 'scholar-sync-co-authors-slug' ), admin_url( 'admin.php' ) );
$action_url  = add_query_arg( array( 'page' => 'scholar-sync-actions-slug', 'action' => 'save-co-author' ), admin_url( 'admin.php' ) );
$save_error  = sanitize_key( $_GET['save_error'] ?? '' );

echo '<div class="wrap">';
echo '<h1>' . esc_html( $form_title ) . '</h1>';

if ( $save_error === 'duplicate_short_name' ) {
    echo '<div class="notice notice-error"><p>' . esc_html__( 'A co-author with this short name already exists.', 'scholar-sync' ) . '</p></div>';
}

echo '<form id="scholar-sync-co-author-form" method="post" action="' . esc_url( $action_url ) . '" data-is-edit="' . ( $is_edit ? '1' : '0' ) . '">';
wp_nonce_field( 'scholar_sync_save_co_author' );
if ( $is_edit ) {
    echo '<input type="hidden" name="co_author_id" value="' . esc_attr( $co_author_id ) . '">';
}

echo '<table class="form-table"><tbody>';

// Full Name
echo '<tr>';
echo '<th scope="row"><label for="full_name">' . esc_html__( 'Full Name', 'scholar-sync' ) . ' <span style="color:red;">*</span></label></th>';
echo '<td><input type="text" id="full_name" name="full_name" class="regular-text" required value="' . esc_attr( $record['full_name'] ) . '"></td>';
echo '</tr>';

// Short Name
echo '<tr>';
echo '<th scope="row"><label for="short_name">' . esc_html__( 'Short Name', 'scholar-sync' ) . ' <span style="color:red;">*</span></label></th>';
echo '<td><input type="text" id="short_name" name="short_name" class="regular-text" required value="' . esc_attr( $record['short_name'] ) . '">';
echo '<p class="description">' . esc_html__( 'Auto-filled from Full Name. Used for matching co-authors in article lists.', 'scholar-sync' ) . '</p>';
echo '</td></tr>';

// Thumbnail Image
$scholar_sync_default_img = SCHOLAR_SYNC_PLUGIN_URL . 'includes/images/blank_profile.png';
$scholar_sync_preview_src = ! empty( $record['image_url'] ) ? $record['image_url'] : $scholar_sync_default_img;
echo '<tr>';
echo '<th scope="row"><label for="thumbnail">' . esc_html__( 'Thumbnail Image', 'scholar-sync' ) . '</label></th>';
echo '<td>';
echo '<input type="text" id="thumbnail" name="image_url" class="regular-text" value="' . esc_attr( $record['image_url'] ?? '' ) . '">';
echo '<button type="button" class="button" id="scholar-sync-select-image" style="margin-left: 4px;">' . esc_html__( 'Select Image', 'scholar-sync' ) . '</button>';
echo '<br>';
echo '<img id="scholar-sync-thumbnail-preview" src="' . esc_url( $scholar_sync_preview_src ) . '" alt="' . esc_attr__( 'Thumbnail preview', 'scholar-sync' ) . '" data-default="' . esc_url( $scholar_sync_default_img ) . '">';
echo '</td>';
echo '</tr>';

// Profile Link
echo '<tr>';
echo '<th scope="row"><label for="profile_link">' . esc_html__( 'Profile Link', 'scholar-sync' ) . '</label></th>';
echo '<td><input type="url" id="profile_link" name="profile_link" class="regular-text" value="' . esc_attr( $record['profile_link'] ?? '' ) . '"></td>';
echo '</tr>';

// Affiliations
echo '<tr>';
echo '<th scope="row"><label for="affiliations">' . esc_html__( 'Affiliations', 'scholar-sync' ) . '</label></th>';
echo '<td><input type="text" id="affiliations" name="affiliations" class="regular-text" value="' . esc_attr( $record['affiliations'] ?? '' ) . '"></td>';
echo '</tr>';

echo '</tbody></table>';

$scholar_sync_co_created = '';
$scholar_sync_co_updated = '';
if ( $is_edit && ! empty( $record['created'] ) ) {
    $scholar_sync_co_created = date_i18n( 'Y-m-d H:i', strtotime( $record['created'] ) );
    $scholar_sync_co_updated = ! empty( $record['last_updated'] )
        ? date_i18n( 'Y-m-d H:i', strtotime( $record['last_updated'] ) ) : '';
}

echo '<p class="submit">';
echo '<input type="submit" class="button button-primary" value="' . esc_attr__( 'Save Co-Author', 'scholar-sync' ) . '">';
echo ' <a href="' . esc_url( $list_url ) . '" class="button">' . esc_html__( 'Cancel', 'scholar-sync' ) . '</a>';
if ( $scholar_sync_co_created ) {
    echo '<span class="scholar-sync-meta-label" style="margin-left:24px;">' . esc_html__( 'Created:', 'scholar-sync' ) . ' <input type="text" readonly class="scholar-sync-meta-input" value="' . esc_attr( $scholar_sync_co_created ) . '"></span>';
    echo '<span class="scholar-sync-meta-label">' . esc_html__( 'Last updated:', 'scholar-sync' ) . ' <input type="text" readonly class="scholar-sync-meta-input" value="' . esc_attr( $scholar_sync_co_updated ) . '"></span>';
}
echo '</p>';
echo '</form>';
echo '</div>';

