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

class FixLinks_Email {

    public static function send_digest(): void {
        if (!FixLinks_License::is_active()) return;
        if (!get_option('fixlinks_email_alerts', '1')) return;
        $pending = FixLinks_DB::count_broken('pending');
        if ($pending === 0) return;
        $subject = sprintf(
            '[%s] FixLinks AI: %d broken link(s) need attention',
            get_bloginfo('name'),
            $pending
        );
        $message = sprintf(
            "Hi,\n\nFixLinks AI weekly report for %s:\n\nBroken: %d  |  Fixed: %d  |  Needs review: %d\n\nLast crawl: %s\n\nFix here:\n%s\n\n— FixLinks AI",
            get_bloginfo('name'),
            FixLinks_DB::count_broken(),
            FixLinks_DB::count_broken('applied'),
            $pending,
            get_option('fixlinks_last_crawl', 'Never'),
            admin_url('admin.php?page=fixlinks-dashboard')
        );
        wp_mail(get_option('admin_email'), $subject, $message);
    }
}