<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- admin view partial; local variables injected from parent include

// variables defined in author_edit_view.php
/** @var object $author_data */
/** @var string $author_id */
$scholar_id = $author_data->scholar_id;
$image_wait_src = SCHOLAR_SYNC_PLUGIN_URL . 'includes/images/wait.gif';
$loading_text   = __( 'Loading...', 'scholar-sync' );

$sync_logs = ScholarSync_Background_Sync_Runner::get_logs_for_author( (int) $author_id );

$outcome_labels = array(
    'success'           => array( __( 'Success',           'scholar-sync' ), '#00a32a' ),
    'skipped_frequency' => array( __( 'Skipped (too soon)', 'scholar-sync' ), '#996800' ),
    'skipped_remaining' => array( __( 'Skipped',           'scholar-sync' ), '#996800' ),
    'error'             => array( __( 'Error',              'scholar-sync' ), '#d63638' ),
);
?>

<div style="padding: 20px 20px 40px;">

    <input type="hidden" id="author-id" name="author-id" value="<?php echo esc_attr( $author_id ); ?>">

    <?php if ( empty( $scholar_id ) ) : ?>

        <p class="description"><?php esc_html_e( 'This researcher was added manually and has no linked Scholar ID, so it cannot be synced.', 'scholar-sync' ); ?></p>

    <?php else : ?>

        <button type="button" class="button" onclick="synAuthor('<?php echo esc_js( $scholar_id ); ?>')"><?php esc_html_e( 'Sync Articles', 'scholar-sync' ); ?></button>

        <div id="sync-results" style="margin-top:12px;"></div>

        <div id="search-author-loader" style="display:none; margin-top:12px;">
            <img id="image-wait" src="<?php echo esc_url( $image_wait_src ); ?>" style="width:60px;height:60px;">
            <div><?php echo esc_html( $loading_text ); ?></div>
        </div>

        <hr style="margin: 24px 0 20px;">
        <h3 style="margin: 0 0 16px;"><?php esc_html_e( 'Sync History', 'scholar-sync' ); ?></h3>

        <?php if ( empty( $sync_logs ) ) : ?>
            <p class="description" id="scholar-sync-no-sync-msg"><?php esc_html_e( 'No sync has run yet for this researcher.', 'scholar-sync' ); ?></p>
        <?php endif; ?>
        <table class="scholar-sync-sync-table" id="scholar-sync-sync-history-table" <?php if ( empty( $sync_logs ) ) echo 'style="display:none;"'; ?>>
            <thead>
                <tr>
                    <th class="col-date"><?php esc_html_e( 'Date',    'scholar-sync' ); ?></th>
                    <th class="col-type"><?php esc_html_e( 'Type',    'scholar-sync' ); ?></th>
                    <th class="col-outcome"><?php esc_html_e( 'Outcome', 'scholar-sync' ); ?></th>
                    <th class="col-num"><?php esc_html_e( 'Added',   'scholar-sync' ); ?></th>
                    <th class="col-num"><?php esc_html_e( 'Updated', 'scholar-sync' ); ?></th>
                    <th><?php esc_html_e( 'Message', 'scholar-sync' ); ?></th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ( $sync_logs as $log ) :
                $outcome = $log->outcome ?? 'error';
                list( $outcome_label, $outcome_color ) = $outcome_labels[ $outcome ] ?? array( $outcome, '#996800' );
                $type = $log->triggered_by === 'manual'
                    ? __( 'Manual', 'scholar-sync' )
                    : __( 'Automatic', 'scholar-sync' );
                $synced_ts = strtotime( $log->synced_at );
            ?>
                <tr>
                    <td class="col-date scholar-sync-local-time" data-utc="<?php echo esc_attr( gmdate( 'c', $synced_ts ) ); ?>"><?php echo esc_html( date_i18n( 'Y-m-d H:i T', $synced_ts ) ); ?></td>
                    <td class="col-type"><?php echo esc_html( $type ); ?></td>
                    <td class="col-outcome"><span style="color:<?php echo esc_attr( $outcome_color ); ?>;font-weight:600;"><?php echo esc_html( $outcome_label ); ?></span></td>
                    <td class="col-num"><?php echo (int) $log->articles_created; ?></td>
                    <td class="col-num"><?php echo (int) $log->articles_updated; ?></td>
                    <td class="col-msg"><?php echo ! empty( $log->error_message ) ? wp_kses( $log->error_message, array( 'a' => array( 'href' => array(), 'target' => array() ) ) ) : '—'; ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>

    <?php endif; ?>

</div>
</div>