<?php
/**
 * Setup-health scorer — tells the user how complete their Brainwerk SEO Suite setup is
 * with plain-language tasks they can tick off.
 *
 * @package Brainwerk SEO Suite
 */

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

class Seoforge_Health {

    /**
     * Returns ['score' => 0..100, 'tasks' => [{label, done, action_url, hint}]].
     */
    public static function check() {
        $tasks = array();
        $s     = function ( $k, $d = false ) { return Seoforge_Settings::get( $k, $d ); };

        // 1. Tracking enabled
        $tasks[] = array(
            'label'      => __( 'Visitor tracking is on', 'brainwerk-seo-suite' ),
            'done'       => (bool) $s( 'tracking_enabled', true ),
            'action_url' => admin_url( 'admin.php?page=brainwerk-seo-suite&tab=privacy' ),
            'hint'       => __( 'Without tracking, all the dashboards stay empty.', 'brainwerk-seo-suite' ),
        );

        // 2. IP anonymization
        $tasks[] = array(
            'label'      => __( 'IPs are anonymized (GDPR)', 'brainwerk-seo-suite' ),
            'done'       => (bool) $s( 'privacy_anonymize_ip', true ),
            'action_url' => admin_url( 'admin.php?page=brainwerk-seo-suite&tab=privacy' ),
            'hint'       => __( 'IP anonymization is a key data-minimization measure; whether you still need a consent banner depends on your setup and should be legally reviewed.', 'brainwerk-seo-suite' ),
        );

        // 3. Sitemap on
        $tasks[] = array(
            'label'      => __( 'XML sitemap is enabled', 'brainwerk-seo-suite' ),
            'done'       => (bool) $s( 'sitemap_enabled', true ),
            'action_url' => admin_url( 'admin.php?page=brainwerk-seo-suite&tab=sitemap' ),
            'hint'       => __( 'Helps Google discover and re-crawl your pages faster.', 'brainwerk-seo-suite' ),
        );

        // 4. Schema on
        $tasks[] = array(
            'label'      => __( 'Schema.org JSON-LD is on', 'brainwerk-seo-suite' ),
            'done'       => (bool) $s( 'seo_schema_enabled', true ),
            'action_url' => admin_url( 'admin.php?page=brainwerk-seo-suite&tab=meta' ),
            'hint'       => __( 'Article + Organization + Breadcrumbs schema for rich-results.', 'brainwerk-seo-suite' ),
        );

        // 5. Organization name set
        $org = (array) $s( 'seo_schema_organization', array() );
        $tasks[] = array(
            'label'      => __( 'Organization name + URL are filled in', 'brainwerk-seo-suite' ),
            'done'       => ! empty( $org['name'] ) && ! empty( $org['url'] ),
            'action_url' => admin_url( 'admin.php?page=brainwerk-seo-suite&tab=meta' ),
            'hint'       => __( 'Used in JSON-LD so Google can show your brand correctly.', 'brainwerk-seo-suite' ),
        );

        // 6. Notification email set
        $tasks[] = array(
            'label'      => __( 'Weekly digest email is configured', 'brainwerk-seo-suite' ),
            'done'       => is_email( $s( 'notify_email_to', '' ) ) && (bool) $s( 'notify_email_enabled', false ),
            'action_url' => admin_url( 'admin.php?page=brainwerk-seo-suite&tab=privacy' ),
            'hint'       => __( 'Get a 7-day report by email — keeps Brainwerk SEO Suite in your routine.', 'brainwerk-seo-suite' ),
        );

        // 7. At least one redirect or 0 broken links
        global $wpdb;
        $r_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i", Seoforge_Utils::table( 'redirects' ) ) );
        $not_resolved = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE is_resolved = 0", Seoforge_Utils::table( '404' ) ) );
        $tasks[] = array(
            'label'      => __( 'No unresolved 404s', 'brainwerk-seo-suite' ),
            'done'       => 0 === $not_resolved,
            'action_url' => admin_url( 'admin.php?page=brainwerk-seo-suite&tab=not-found' ),
            'hint'       => $not_resolved > 0
                ? sprintf(
                    /* translators: %d: number of URLs currently returning 404 */
                    __( '%d URLs are returning 404 — fix or redirect them.', 'brainwerk-seo-suite' ),
                    $not_resolved
                )
                : __( 'All known 404s are resolved.', 'brainwerk-seo-suite' ),
        );

        // 8. SEO scores filled
        $missing_meta = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE score < 50", Seoforge_Utils::table( 'meta' ) ) );
        $tasks[] = array(
            'label'      => __( 'No posts with a critical SEO score', 'brainwerk-seo-suite' ),
            'done'       => 0 === $missing_meta,
            'action_url' => admin_url( 'admin.php?page=brainwerk-seo-suite&tab=seo' ),
            'hint'       => $missing_meta > 0
                ? sprintf(
                    /* translators: %d: number of posts with on-page SEO score below 50 */
                    __( '%d posts score below 50 — fix titles, meta descriptions and focus keywords.', 'brainwerk-seo-suite' ),
                    $missing_meta
                )
                : __( 'Every analyzed post has a healthy on-page score.', 'brainwerk-seo-suite' ),
        );

        $done = 0;
        foreach ( $tasks as $t ) { if ( $t['done'] ) { $done++; } }
        $score = (int) round( ( $done / max( 1, count( $tasks ) ) ) * 100 );

        return array( 'score' => $score, 'tasks' => $tasks );
    }
}
