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

function lt_core_register_health_menu() {
    add_submenu_page( 'lt-core-dashboard', __( 'Site Health', 'law-temple-core' ), __( 'Site Health', 'law-temple-core' ), 'lt_manage_site_health', 'lt-core-health', 'lt_core_health_page' );
}
add_action( 'admin_menu', 'lt_core_register_health_menu', 35 );

function lt_core_health_checks() {
    $front = absint( get_option( 'page_on_front' ) );
    $privacy = absint( lt_core_option( 'privacy_page_id' ) ?: get_option( 'wp_page_for_privacy_policy' ) );
    $schema = (bool) lt_core_option( 'schema_enabled' );
    $checks = array(
        array( __( 'Firm name configured', 'law-temple-core' ), (bool) trim( (string) lt_core_option( 'firm_name' ) ), __( 'Set the firm name under Law Firm → Firm Settings.', 'law-temple-core' ) ),
        array( __( 'Homepage configured', 'law-temple-core' ), $front && 'page' === get_option( 'show_on_front' ), __( 'Choose a static homepage.', 'law-temple-core' ) ),
        array( __( 'Privacy page configured', 'law-temple-core' ), $privacy && 'publish' === get_post_status( $privacy ), __( 'Select a published Privacy Notice page.', 'law-temple-core' ) ),
        array( __( 'Enquiry recipient configured', 'law-temple-core' ), is_email( lt_core_option( 'enquiry_recipient' ) ), __( 'Set a valid enquiry recipient email.', 'law-temple-core' ) ),
        array( __( 'At least one office', 'law-temple-core' ), (bool) wp_count_posts( 'office' )->publish, __( 'Create an office record or intentionally operate without office pages.', 'law-temple-core' ) ),
        array( __( 'At least one Practice Area', 'law-temple-core' ), (bool) wp_count_posts( 'practice' )->publish, __( 'Publish at least one Practice Area.', 'law-temple-core' ) ),
        array( __( 'At least one professional profile', 'law-temple-core' ), (bool) wp_count_posts( 'lawyer' )->publish, __( 'Publish at least one team profile.', 'law-temple-core' ) ),
        array( __( 'Permalinks enabled', 'law-temple-core' ), (bool) get_option( 'permalink_structure' ), __( 'Use a pretty permalink structure for clean URLs.', 'law-temple-core' ) ),
        array( __( 'Supported PHP version', 'law-temple-core' ), version_compare( PHP_VERSION, '8.0', '>=' ), sprintf( __( 'Current PHP version: %s', 'law-temple-core' ), PHP_VERSION ) ),
        array( __( 'Built-in schema state', 'law-temple-core' ), ! $schema || ! lt_core_has_seo_plugin(), $schema && lt_core_has_seo_plugin() ? __( 'An SEO plugin is active; built-in schema is automatically suppressed to avoid duplication.', 'law-temple-core' ) : __( 'Schema configuration is compatible with the current plugin state.', 'law-temple-core' ) ),
        array( __( 'HTTPS', 'law-temple-core' ), is_ssl(), is_ssl() ? __( 'The current administration request is using HTTPS.', 'law-temple-core' ) : __( 'Use HTTPS on staging and production before accepting client enquiries.', 'law-temple-core' ) ),
        array( __( 'WordPress environment type', 'law-temple-core' ), function_exists('wp_get_environment_type') ? in_array(wp_get_environment_type(),array('local','development','staging','production'),true) : true, function_exists('wp_get_environment_type') ? sprintf( __( 'Current environment: %s', 'law-temple-core' ), wp_get_environment_type() ) : __( 'Environment type API unavailable.', 'law-temple-core' ) ),
        array( __( 'Uploads available', 'law-temple-core' ), wp_max_upload_size() > 0, sprintf( __( 'Maximum upload size reported by WordPress: %s', 'law-temple-core' ), size_format( wp_max_upload_size() ) ) ),
        array( __( 'Relationship index available', 'law-temple-core' ), function_exists('lt_core_relationship_table_exists') && lt_core_relationship_table_exists(), __( 'v3.0.0 uses an indexed relationship table for scalable reverse lookups while retaining legacy relationship meta.', 'law-temple-core' ) ),
        array( __( 'Role capability model synchronized', 'law-temple-core' ), (string) get_option( 'lt_core_roles_version', '' ) === ( defined( 'LT_CORE_VERSION' ) ? LT_CORE_VERSION : '' ) && (bool) get_role( 'lt_firm_administrator' ) && (bool) get_role( 'lt_content_editor' ), __( 'Synchronize Law Firm roles from Law Firm → Roles & Permissions after a migration or role reset.', 'law-temple-core' ) ),
        array( __( 'Search configuration valid', 'law-temple-core' ), (bool) trim( (string) lt_core_option( 'search_types' ) ) && absint( lt_core_option( 'search_max_results' ) ?: 8 ) >= 1 && absint( lt_core_option( 'search_max_results' ) ?: 8 ) <= 20, __( 'Review searchable content types and live-search limits under Law Firm → Firm Settings → Search.', 'law-temple-core' ) ),
        array( __( 'Structured editorial data available', 'law-temple-core' ), function_exists( 'lt_core_professional_schema' ) && function_exists( 'lt_core_structured_content_schema' ), __( 'v3.0.0 supports structured professional credentials plus structured Practice Area and Representative Matter records with legacy fallbacks.', 'law-temple-core' ) ),
    );
    return apply_filters( 'lt_core_health_checks', $checks );
}

function lt_core_health_page() {
    if ( ! lt_core_current_user_can( 'lt_manage_site_health' ) ) { return; }
    echo '<div class="wrap"><h1>' . esc_html__( 'Law Firm Site Health', 'law-temple-core' ) . '</h1><p>' . esc_html__( 'Configuration checks for the law-firm framework. These checks do not replace WordPress Site Health, SMTP testing, accessibility testing or browser QA.', 'law-temple-core' ) . '</p><table class="widefat striped" style="max-width:1000px"><thead><tr><th>' . esc_html__( 'Check', 'law-temple-core' ) . '</th><th>' . esc_html__( 'Status', 'law-temple-core' ) . '</th><th>' . esc_html__( 'Guidance', 'law-temple-core' ) . '</th></tr></thead><tbody>';
    foreach ( lt_core_health_checks() as $row ) {
        echo '<tr><td><strong>' . esc_html( $row[0] ) . '</strong></td><td>' . ( $row[1] ? '<span style="color:#087f23">✓ ' . esc_html__( 'Pass', 'law-temple-core' ) . '</span>' : '<span style="color:#b32d2e">⚠ ' . esc_html__( 'Review', 'law-temple-core' ) . '</span>' ) . '</td><td>' . esc_html( $row[2] ) . '</td></tr>';
    }
    echo '</tbody></table></div>';
}
