<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>
<div class="wrap">
    <style>
        .sbs-admin-table-wrap {
            width: 100%;
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
        }
        .sbs-admin-table-wrap .wp-list-table {
            min-width: 980px;
        }
    </style>
    <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
    
    <?php if (empty($logs)) : ?>
        <div class="notice notice-info">
            <p><?php esc_html_e('No sync logs available yet.', 'yatoon-booking-system'); ?></p>
        </div>
    <?php else : ?>
        <div class="sbs-admin-table-wrap">
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th class="manage-column"><?php esc_html_e('Timestamp', 'yatoon-booking-system'); ?></th>
                    <th class="manage-column"><?php esc_html_e('Status', 'yatoon-booking-system'); ?></th>
                    <th class="manage-column"><?php esc_html_e('Details', 'yatoon-booking-system'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($logs as $log) : ?>
                    <tr>
                        <td><?php echo esc_html($log['timestamp']); ?></td>
                        <td>
                            <?php if ($log['status'] === 'completed') : ?>
                                <span class="sbs-status-success"><span aria-hidden="true">&#10003;</span> <?php esc_html_e('Completed', 'yatoon-booking-system'); ?></span>
                            <?php elseif ($log['status'] === 'failed') : ?>
                                <span class="sbs-status-error">No <?php esc_html_e('Failed', 'yatoon-booking-system'); ?></span>
                            <?php else : ?>
                                <span class="sbs-status-pending">⟳ <?php esc_html_e('In Progress', 'yatoon-booking-system'); ?></span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if (isset($log['details'])) : ?>
                                <details>
                                    <summary style="cursor: pointer; color: #4F46E5;">
                                        <?php esc_html_e('View Details', 'yatoon-booking-system'); ?>
                                    </summary>
                                    <div style="margin-top: 10px; padding: 10px; background: #f9fafb; border-radius: 4px;">
                                        <?php if (isset($log['details']['locations'])) : ?>
                                            <p><strong><?php esc_html_e('Locations:', 'yatoon-booking-system'); ?></strong> 
                                                <?php echo esc_html($log['details']['locations']['synced'] ?? 0); ?> synced
                                            </p>
                                        <?php endif; ?>
                                        
                                        <?php if (isset($log['details']['services'])) : ?>
                                            <p><strong><?php esc_html_e('Services:', 'yatoon-booking-system'); ?></strong> 
                                                <?php echo esc_html($log['details']['services']['services'] ?? 0); ?> services, 
                                                <?php echo esc_html($log['details']['services']['options'] ?? 0); ?> options
                                            </p>
                                        <?php endif; ?>
                                        
                                        <?php if (isset($log['details']['staff'])) : ?>
                                            <p><strong><?php esc_html_e('Staff:', 'yatoon-booking-system'); ?></strong> 
                                                <?php echo esc_html($log['details']['staff']['synced'] ?? 0); ?> synced
                                            </p>
                                        <?php endif; ?>
                                        
                                        <?php if (isset($log['details']['bookings'])) : ?>
                                            <p><strong><?php esc_html_e('Bookings:', 'yatoon-booking-system'); ?></strong> 
                                                <?php echo esc_html($log['details']['bookings']['synced'] ?? 0); ?> synced
                                            </p>
                                        <?php endif; ?>
                                        
                                        <?php if (isset($log['error'])) : ?>
                                            <p style="color: #EF4444;">
                                                <strong><?php esc_html_e('Error:', 'yatoon-booking-system'); ?></strong><br>
                                                <?php echo esc_html($log['error']); ?>
                                            </p>
                                        <?php endif; ?>
                                    </div>
                                </details>
                            <?php else : ?>
                                -
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        </div>
    <?php endif; ?>
    
    <p class="description">
        <?php esc_html_e('This log shows the history of synchronization activities between WordPress and Square.', 'yatoon-booking-system'); ?>
    </p>
</div>

<style>
.sbs-status-success {
    color: #10B981;
    font-weight: 600;
}
.sbs-status-error {
    color: #EF4444;
    font-weight: 600;
}
.sbs-status-pending {
    color: #F59E0B;
    font-weight: 600;
}
</style>
