<?php
/**
 * Configurations List View
 *
 * @package SlideMind
 * @since 2.0.0
 */

if (!defined('ABSPATH')) {
    exit;
}

// Fetch configurations via plugin
$plugin = \SlideMind\Plugin::instance();
$config_repo = $plugin->get_config_repository();
$configs = $config_repo->get_all(['post_status' => 'any']);

// Quick stats
function slidemind_get_config_stats($config_id) {
    global $wpdb;
    $table_logs = $wpdb->prefix . 'slidemind_logs';
    // phpcs:disable WordPress.DB.DirectDatabaseQuery
    return $wpdb->get_row($wpdb->prepare(
        "SELECT COUNT(*) as calls, SUM(estimated_cost) as cost
        FROM %i WHERE config_id = %d",
        $table_logs,
        $config_id
    ));
    // phpcs:enable WordPress.DB.DirectDatabaseQuery
}
?>

<div class="sm-wrap">
    <div class="sm-header">
        <h1><?php esc_html_e('Slides', 'slidemind'); ?></h1>
        <div class="sm-header-actions">
            <a href="<?php echo esc_url(admin_url('admin.php?page=slidemind-new')); ?>" class="sm-btn sm-btn-primary">
                <span class="dashicons dashicons-plus-alt2"></span>
                <?php esc_html_e('New slide', 'slidemind'); ?>
            </a>
        </div>
    </div>

    <div class="sm-card">
        <?php if (empty($configs)): ?>
            <div class="sm-empty-state">
                <svg class="sm-empty-state-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                </svg>
                <h3 class="sm-empty-state-title"><?php esc_html_e('No slides', 'slidemind'); ?></h3>
                <p class="sm-empty-state-description"><?php esc_html_e('Create your first slide to get started.', 'slidemind'); ?></p>
                <a href="<?php echo esc_url(admin_url('admin.php?page=slidemind-new')); ?>" class="sm-btn sm-btn-primary">
                    <?php esc_html_e('Create a slide', 'slidemind'); ?>
                </a>
            </div>
        <?php else: ?>
            <table class="sm-table">
                <thead>
                    <tr>
                        <th><?php esc_html_e('Course', 'slidemind'); ?></th>
                        <th><?php esc_html_e('Model', 'slidemind'); ?></th>
                        <th><?php esc_html_e('Integration code', 'slidemind'); ?></th>
                        <th><?php esc_html_e('Status', 'slidemind'); ?></th>
                        <th><?php esc_html_e('Calls', 'slidemind'); ?></th>
                        <th><?php esc_html_e('Cost', 'slidemind'); ?></th>
                        <th><?php esc_html_e('Actions', 'slidemind'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($configs as $config):
                        $stats = slidemind_get_config_stats($config['id']);
                    ?>
                        <tr>
                            <td>
                                <strong><?php echo esc_html($config['title'] ?: $config['course_name']); ?></strong>
                                <?php if ($config['usage_scope'] !== 'course'): ?>
                                    <br><small style="color: #6b7280;">
                                        <?php echo esc_html(ucfirst($config['usage_scope'])); ?>:
                                        <?php echo esc_html($config['module_info']); ?>
                                        <?php if ($config['slide_info']): ?>
                                            / <?php echo esc_html($config['slide_info']); ?>
                                        <?php endif; ?>
                                    </small>
                                <?php endif; ?>
                            </td>
                            <td>
                                <code style="font-size: 12px;"><?php echo esc_html($config['model_id']); ?></code>
                            </td>
                            <td>
                                <div class="sm-copyable">
                                    <code><?php echo esc_html($config['config_uid']); ?></code>
                                    <button type="button" class="sm-copyable-btn" data-slug="<?php echo esc_attr($config['config_uid']); ?>" title="<?php esc_attr_e('Copy', 'slidemind'); ?>">
                                        <span class="dashicons dashicons-clipboard"></span>
                                    </button>
                                </div>
                            </td>
                            <td>
                                <?php if ($config['status'] === 'publish'): ?>
                                    <span class="sm-badge sm-badge-success"><?php esc_html_e('Active', 'slidemind'); ?></span>
                                <?php elseif ($config['status'] === 'draft'): ?>
                                    <span class="sm-badge sm-badge-warning"><?php esc_html_e('Draft', 'slidemind'); ?></span>
                                <?php else: ?>
                                    <span class="sm-badge"><?php echo esc_html($config['status']); ?></span>
                                <?php endif; ?>

                                <?php if ($config['has_custom_key']): ?>
                                    <span class="sm-badge sm-badge-info" title="<?php esc_attr_e('Uses a custom API key', 'slidemind'); ?>"><?php esc_html_e('Custom key', 'slidemind'); ?></span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo esc_html(number_format($stats->calls ?: 0)); ?></td>
                            <td>$<?php echo esc_html(number_format($stats->cost ?: 0, 4)); ?></td>
                            <td>
                                <div class="sm-table-actions">
                                    <a href="<?php echo esc_url(admin_url('admin.php?page=slidemind-new&config_id=' . $config['id'])); ?>"
                                       class="sm-btn sm-btn-sm sm-btn-secondary" title="<?php esc_attr_e('Edit', 'slidemind'); ?>">
                                        <span class="dashicons dashicons-edit"></span>
                                    </a>
                                    <a href="<?php echo esc_url(admin_url('admin.php?page=slidemind-sandbox&config_id=' . $config['id'])); ?>"
                                       class="sm-btn sm-btn-sm sm-btn-secondary" title="<?php esc_attr_e('Test', 'slidemind'); ?>">
                                        <span class="dashicons dashicons-visibility"></span>
                                    </a>
                                    <button type="button"
                                            class="sm-btn sm-btn-sm sm-btn-secondary sm-delete-config"
                                            data-config-id="<?php echo esc_attr($config['id']); ?>"
                                            title="<?php esc_attr_e('Delete', 'slidemind'); ?>">
                                        <span class="dashicons dashicons-trash"></span>
                                    </button>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
</div>
