<?php
if (!defined('ABSPATH')) exit;

class WPBS_Accessibility {
    
    public static function render_dashboard() {
        // Get all Bootstrap Studio pages
        $pages = get_posts([
            'post_type' => 'page',
            'posts_per_page' => -1,
            'meta_key' => '_wpbs_enabled',
            'meta_value' => '1',
        ]);
        ?>
        <div class="wrap">
            <h1>Accessibility Dashboard</h1>
            <p>Accessibility reports for your Bootstrap Studio pages. Reports are generated when pages are built or validated through the API.</p>
            
            <?php if (empty($pages)) : ?>
                <div class="notice notice-info">
                    <p>No Bootstrap Studio pages found. Enable Bootstrap Studio on a page to see accessibility reports here.</p>
                </div>
            <?php else : ?>
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th>Page</th>
                            <th>Status</th>
                            <th>Score</th>
                            <th>Violations</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($pages as $page) : 
                            $report = get_post_meta($page->ID, '_wpbs_a11y_report', true);
                        ?>
                        <tr>
                            <td>
                                <strong><a href="<?php echo get_edit_post_link($page->ID); ?>"><?php echo esc_html($page->post_title ?: '(no title)'); ?></a></strong>
                            </td>
                            <td>
                                <?php if ($report) : ?>
                                    <span style="color: green;">✓ Scanned</span>
                                <?php else : ?>
                                    <span style="color: #999;">Not scanned</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($report && isset($report['score'])) : ?>
                                    <strong><?php echo esc_html($report['score']); ?>/100</strong>
                                <?php else : ?>
                                    —
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($report && isset($report['violations'])) : ?>
                                    <?php echo count($report['violations']); ?> issue(s)
                                <?php else : ?>
                                    —
                                <?php endif; ?>
                            </td>
                            <td>
                                <a href="<?php echo get_edit_post_link($page->ID); ?>" class="button button-small">Edit Page</a>
                                <a href="<?php echo esc_url(get_permalink($page->ID)); ?>" class="button button-small" target="_blank">View</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
            
            <hr>
            <h2>About Accessibility Checks</h2>
            <p>WP Bootstrap Studio uses <strong>axe-core</strong> (the industry standard) to audit pages for WCAG 2.1 AA compliance. Checks run automatically when pages are generated or validated through the Vercel API.</p>
            <p>The AI page builder is also prompted to generate accessible HTML by default — proper heading hierarchy, alt text, ARIA labels, semantic elements, and color contrast.</p>
        </div>
        <?php
    }
}
