<?php
defined('ABSPATH') || exit;

class FixLinks_Report {

    public static function export(): void {
        if (!current_user_can('manage_options')) wp_die('Forbidden');
        check_admin_referer('fixlinks_export_report');
        if (FixLinks_License::get_plan() !== 'agency') {
            wp_die(__('Agency license required.', 'fixlinks-ai'));
        }

        $client    = sanitize_text_field(wp_unslash($_GET['client'] ?? get_bloginfo('name')));
        $broken    = FixLinks_DB::get_broken_links(null, 500);
        $redirects = FixLinks_DB::get_all_redirects();

        header('Content-Type: text/html; charset=UTF-8');
        header('Content-Disposition: inline; filename="fixlinks-report-' . gmdate('Y-m-d') . '.html"');
        ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Broken Link Report — <?php echo esc_html($client) ?></title>
<style>
*{box-sizing:border-box;margin:0;padding:0}body{font-family:'Segoe UI',Arial,sans-serif;font-size:13px;color:#1F2937;background:#F9FAFB}
a{color:#4F46E5}code{font-size:11px;background:#F1F5F9;padding:2px 5px;border-radius:3px;word-break:break-all}
.page{max-width:960px;margin:0 auto;padding:32px 24px}
.hdr{display:flex;justify-content:space-between;padding-bottom:24px;border-bottom:2px solid #4F46E5;margin-bottom:28px}
.hdr h1{font-size:22px;font-weight:800;color:#4F46E5}.hdr .meta{font-size:12px;color:#6B7280;line-height:1.8}
.hdr .logo{font-size:11px;color:#9CA3AF;text-align:right}.hdr .logo strong{display:block;font-size:14px;color:#4F46E5}
.summary{display:grid;grid-template-columns:repeat(4,1fr);gap:16px;margin-bottom:32px}
.sc{background:#fff;border:1px solid #E5E7EB;border-radius:8px;padding:16px}
.sc .num{font-size:32px;font-weight:800}.sc .lbl{font-size:11px;color:#6B7280;text-transform:uppercase;margin-top:4px}
.r{color:#DC2626}.g{color:#059669}.b{color:#2563EB}.p{color:#7C3AED}
h2{font-size:15px;font-weight:700;margin:28px 0 12px;border-left:3px solid #4F46E5;padding-left:10px}
table{width:100%;border-collapse:collapse;background:#fff;border-radius:8px;border:1px solid #E5E7EB;margin-bottom:24px}
th{background:#F8FAFC;font-size:11px;text-transform:uppercase;color:#6B7280;font-weight:700;padding:10px 12px;text-align:left;border-bottom:1px solid #E5E7EB}
td{padding:10px 12px;border-bottom:1px solid #F3F4F6;vertical-align:top}tr:last-child td{border-bottom:none}
.b404{background:#fee2e2;color:#b91c1c}.b5xx{background:#fef3c7;color:#92400e}.b0{background:#f1f5f9;color:#64748b}
.bdg,.bh{display:inline-block;padding:2px 8px;border-radius:12px;font-size:10px;font-weight:700;text-transform:uppercase}
.applied{background:#d1fae5;color:#065f46}.pending{background:#fef3c7;color:#92400e}
.high{background:#d1fae5;color:#065f46}.medium{background:#fef3c7;color:#92400e}.low{background:#fee2e2;color:#b91c1c}
.footer{margin-top:40px;padding-top:20px;border-top:1px solid #E5E7EB;font-size:11px;color:#9CA3AF;display:flex;justify-content:space-between}
.print-btn{position:fixed;bottom:24px;right:24px;background:#4F46E5;color:#fff;border:none;padding:12px 24px;border-radius:8px;font-size:14px;font-weight:600;cursor:pointer}
@media print{.print-btn{display:none!important}body{background:#fff}}
</style>
</head>
<body>
<div class="page">
  <div class="hdr">
    <div>
      <h1>Broken Link Report</h1>
      <div class="meta">
        <strong><?php echo esc_html($client) ?></strong><br>
        <a href="<?php echo esc_url(get_site_url()) ?>"><?php echo esc_html(get_site_url()) ?></a><br>
        Generated: <?php echo esc_html( gmdate('d F Y H:i') ) ?> | Last crawl: <?php echo esc_html(get_option('fixlinks_last_crawl', 'N/A')) ?>
      </div>
    </div>
    <div class="logo"><strong>FixLinks AI</strong><br>Broken Link Management</div>
  </div>

  <div class="summary">
    <div class="sc"><div class="num r"><?php echo absint( FixLinks_DB::count_broken() ) ?></div><div class="lbl">Total Broken</div></div>
    <div class="sc"><div class="num p"><?php echo absint( FixLinks_DB::count_broken('pending') ) ?></div><div class="lbl">Needs Review</div></div>
    <div class="sc"><div class="num g"><?php echo absint( FixLinks_DB::count_broken('applied') ) ?></div><div class="lbl">Fixed (301)</div></div>
    <div class="sc"><div class="num b"><?php echo absint( count($redirects) ) ?></div><div class="lbl">Active Redirects</div></div>
  </div>

  <h2>Broken Links</h2>
  <?php if (empty($broken)): ?>
    <p style="color:#6B7280;padding:16px 0">No broken links. 🎉</p>
  <?php else: ?>
  <table>
    <thead>
      <tr>
        <th>Broken URL</th>
        <th>Found on Page</th>
        <th>Code</th>
        <th>AI Suggestion</th>
        <th>Status</th>
      </tr>
    </thead>
    <tbody>
    <?php foreach ($broken as $link):
        $cc = $link->http_status === 404 ? 'b404' : ($link->http_status >= 500 ? 'b5xx' : 'b0'); ?>
    <tr>
      <td>
        <code><?php echo esc_html($link->broken_url) ?></code><br>
        <small style="color:#9CA3AF"><?php echo esc_html($link->anchor_text) ?></small>
      </td>
      <td><a href="<?php echo esc_url($link->source_page) ?>" target="_blank"><?php echo esc_html( wp_parse_url( $link->source_page, PHP_URL_PATH ) ) ?></a></td>
      <td><span class="bdg <?php echo esc_attr($cc) ?>"><?php echo $link->http_status ? absint( $link->http_status ) : 'Timeout' ?></span></td>
      <td>
        <?php if ($link->ai_suggestion): ?>
          <a href="<?php echo esc_url($link->ai_suggestion) ?>" style="font-size:11px" target="_blank"><?php echo esc_html($link->ai_suggestion) ?></a><br>
          <span class="bh <?php echo esc_attr($link->confidence ?? 'low') ?>"><?php echo esc_html(ucfirst($link->confidence ?? 'low')) ?></span>
        <?php else: ?>
          <span style="color:#9CA3AF">—</span>
        <?php endif; ?>
      </td>
      <td><span class="bdg <?php echo esc_attr($link->status) ?>"><?php echo esc_html(ucfirst($link->status)) ?></span></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
  </table>
  <?php endif; ?>

  <?php if (!empty($redirects)): ?>
  <h2>Active 301 Redirects</h2>
  <table>
    <thead>
      <tr><th>From Path</th><th>To URL</th><th>Hits</th><th>Applied</th></tr>
    </thead>
    <tbody>
    <?php foreach ($redirects as $r): ?>
    <tr>
      <td><code><?php echo esc_html($r->from_path) ?></code></td>
      <td><a href="<?php echo esc_url($r->to_url) ?>"><?php echo esc_html($r->to_url) ?></a></td>
      <td><?php echo (int)$r->hit_count ?></td>
      <td><?php echo esc_html(substr($r->created_at, 0, 10)) ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
  </table>
  <?php endif; ?>

  <div class="footer">
    <span><?php echo esc_html($client) ?> · <?php echo esc_html(get_site_url()) ?></span>
    <span>FixLinks AI · <?php echo esc_html( gmdate('d F Y') ) ?></span>
  </div>
</div>
<button class="print-btn" onclick="window.print()">🖨 Print / Save as PDF</button>
</body>
</html>
        <?php exit;
    }
}