<?php
include('config.php');

$regulators = $_GET['regulators'] ?? [];
$limit = 10;
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$offset = ($page - 1) * $limit;

// Prepare placeholders
$placeholders = implode(',', array_fill(0, count($regulators), '?'));

// Count query
$countSql = !empty($regulators)
    ? "SELECT COUNT(*) FROM brokers WHERE regulator IN ($placeholders)"
    : "SELECT COUNT(*) FROM brokers";
$countStmt = $dbh->prepare($countSql);
!empty($regulators) ? $countStmt->execute($regulators) : $countStmt->execute();
$totalRows = $countStmt->fetchColumn();
$totalPages = ceil($totalRows / $limit);

// Fetch paginated results
$sql = !empty($regulators)
    ? "SELECT * FROM brokers WHERE regulator IN ($placeholders) LIMIT $limit OFFSET $offset"
    : "SELECT * FROM brokers LIMIT $limit OFFSET $offset";
$stmt = $dbh->prepare($sql);
!empty($regulators) ? $stmt->execute($regulators) : $stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
    * {
        margin: 0;
        padding: 0;
    }

    .shield-badge {
        width: 40px;
        height: 50px;
        background: #ffc107;
        color: #333;
        font-weight: bold;
        font-size: 20px;
        text-align: center;
        line-height: 50px;
        clip-path: polygon(50% 0%, 100% 25%, 100% 80%, 50% 100%, 0% 80%, 0% 25%);
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    }

    .broker-card {
        border: 1px solid #ddd;
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 25px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    }

    .broker-logo {
        max-height: 60px;
    }

    .badge-green {
        background-color: #28a745;
        color: white;
        font-size: 13px;
        font-weight: 500;
        padding: 4px 10px;
    }

    .tag-badge {
        background-color: #eaf2ff;
        color: #333;
        border-radius: 5px;
        padding: 3px 8px;
        margin: 2px;
        font-size: 12px;
    }

    .score-box {
        font-size: 24px;
        font-weight: bold;
        color: #b8860b;
    }

    @media (max-width: 768px) {
        * {
            margin: 0;
            padding: 0;
        }

        .broker-card {
            padding: 15px;
        }

        .broker-logo {
            max-height: 50px;
        }

        .badge-green {
            font-size: 12px;
            padding: 3px 8px;
        }

        .score-box {
            font-size: 24px;
        }
    }

    @media (max-width: 576px) {
        .broker-card {
            padding: 10px;
        }

        .broker-logo {
            max-height: 40px;
        }

        .score-box {
            font-size: 20px;
        }

        .tag-badge {
            font-size: 11px;
        }
    }
</style>
<?php //include "includes/header.php";
?>
<div class="container py-4">
    <?php if (empty($results)): ?>
        <div class="alert alert-warning">No brokers found for the selected regulator(s).</div>
    <?php else: ?>
        <?php foreach ($results as $index => $row): ?>
            <div class="broker-card-wrapper mb-4" data-aos="fade-up">
                <div class="broker-card row align-items-center">
                    <!-- Logo and Regulated Badge -->
                    <div class="col-4 col-md-2 text-center mb-3 mb-md-0">
                        <div class="badge-green mb-2">Regulated</div>
                        <div class="broker-logo-wrapper">
                            <img src="./uploads/<?= htmlentities($row['logo'] ?: 'default-logo.png') ?>" alt="Logo" class="broker-logo">
                        </div>
                    </div>

                    <!-- Main Info -->
                    <div class="col-8 col-md-7">
                        <h5 class="mb-1"><?= htmlentities($row['name']) ?></h5>
                        <div class="d-flex align-items-center flex-wrap mb-2 text-muted small">
                            <img src="<?= htmlentities($row['flag_url'] ?? 'https://flagcdn.com/w40/us.png') ?>" width="22" class="me-2" alt="Flag">
                            <span class="me-3"><?= htmlentities($row['country'] ?? 'Unknown') ?></span>
                            <span class="me-3"><?= htmlentities($row['experience'] ?? 'N/A') ?> years</span>
                            <span class="badge bg-light border text-dark" id="hi"><?= htmlentities($row['rating'] ?? 'N/A') ?></span>
                        </div>
                        <style>
                            @media(max-width:768px) {
                                #hi {
                                    content-visibility: hidden;
                                }
                            }
                        </style>
                        <div>
                            <?php if (!empty($row['license1'])): ?>
                                <div><a href="#"><?= htmlentities($row['license1']) ?></a><span class="badge bg-success ms-1">Regulated</span></div>
                            <?php endif; ?>
                            <?php if (!empty($row['license2'])): ?>
                                <div><a href="#"><?= htmlentities($row['license2']) ?></a><span class="badge bg-success ms-1">Regulated</span></div>
                            <?php endif; ?>
                        </div>

                        <div class="mt-2">
                            <?php foreach (explode(',', $row['regulation_areas'] ?? '') as $area): ?>
                                <?php if (trim($area)): ?>
                                    <span class="tag-badge">Regulated in <?= htmlentities(trim($area)) ?></span>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </div>

                        <div class="text-muted small mt-2">
                            Regulatory License Number: <?= htmlentities($row['license_number'] ?? 'N/A') ?>
                        </div>
                    </div>

                    <!-- Score -->
                    <div class="col-6 col-md-2 text-center text-md-end mt-3 mt-md-0">
                        <div class="score-box"><?= htmlentities($row['score']) ?></div>
                        <div class="small">Score</div>
                    </div>

                    <!-- Rank Badge -->
                    <div class="col-6 col-md-1 text-center text-md-end mt-3 mt-md-0">
                        <div class="shield-badge mx-auto"><?= $offset + $index + 1 ?></div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>

    <!-- Pagination -->
    <?php if ($totalPages > 1): ?>
        <nav>
            <ul class="pagination justify-content-center mt-4">
                <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                    <li class="page-item <?= ($i === $page) ? 'active' : '' ?>">
                        <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => $i])) ?>"><?= $i ?></a>
                    </li>
                <?php endfor; ?>
            </ul>
        </nav>
    <?php endif; ?>
</div>