<?php
/**
 * Template: Compliance Report
 *
 * Mirrors the dashboard app's compliance view: a jurisdiction header (region,
 * framework, consent model), an overall score, and the jurisdiction-aware list
 * of checks with four statuses (pass / warning / fail / info-reminder). ALL
 * dynamic strings AND the score-card / status labels arrive already localized
 * from the get-compliance-report proxy (single source = the app's own i18n), so
 * the screen is fully localized in every language with no per-string drift.
 *
 * @var array $data JSON data from Cloud API (get-compliance-report response).
 * @package Biscotti_CMP
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template is included inside Biscotti_Admin::render_dynamic_page(); these are function-scoped locals, not globals.

$page_title = __( 'Compliance Report', 'biscotti-cmp' );
include __DIR__ . '/partials/page-header.php';

if ( is_wp_error( $data ) ) :
	$error_message = $data->get_error_message();
	include __DIR__ . '/partials/error-notice.php';
	return;
endif;

if ( empty( $data ) ) :
	include __DIR__ . '/partials/loading-state.php';
	return;
endif;

$score          = isset( $data['score'] ) ? absint( $data['score'] ) : 0;
$checks         = isset( $data['checks'] ) && is_array( $data['checks'] ) ? $data['checks'] : array();
$region_label   = isset( $data['regionLabel'] ) ? (string) $data['regionLabel'] : '';
$region_emoji   = isset( $data['regionEmoji'] ) ? (string) $data['regionEmoji'] : '';
$model_label    = isset( $data['modelLabel'] ) ? (string) $data['modelLabel'] : '';
$framework      = isset( $data['framework'] ) ? (string) $data['framework'] : '';
$jur_name       = isset( $data['jurisdictionName'] ) ? (string) $data['jurisdictionName'] : '';
$no_requirement = ! empty( $data['noRequirement'] );
$pass_count     = isset( $data['passCount'] ) ? absint( $data['passCount'] ) : 0;
$warn_count     = isset( $data['warningCount'] ) ? absint( $data['warningCount'] ) : 0;
$fail_count     = isset( $data['failCount'] ) ? absint( $data['failCount'] ) : 0;

// Localized chrome from the server (single source). Fall back to the plugin
// textdomain only if the proxy did not provide a label.
$L  = isset( $data['labels'] ) && is_array( $data['labels'] ) ? $data['labels'] : array();
$SL = isset( $data['statusLabels'] ) && is_array( $data['statusLabels'] ) ? $data['statusLabels'] : array();
$lbl_score   = isset( $L['score'] ) ? (string) $L['score'] : __( 'Compliance Score', 'biscotti-cmp' );
$lbl_passed  = isset( $L['passed'] ) ? (string) $L['passed'] : __( 'Passed', 'biscotti-cmp' );
$lbl_warn    = isset( $L['warnings'] ) ? (string) $L['warnings'] : __( 'Warnings', 'biscotti-cmp' );
$lbl_action  = isset( $L['actionRequired'] ) ? (string) $L['actionRequired'] : __( 'Action required', 'biscotti-cmp' );
$lbl_checks  = isset( $L['checksHeading'] ) ? (string) $L['checksHeading'] : __( 'Compliance Checks', 'biscotti-cmp' );

if ( $score >= 80 ) {
	$score_icon = 'dashicons-yes-alt';
} elseif ( $score >= 50 ) {
	$score_icon = 'dashicons-warning';
} else {
	$score_icon = 'dashicons-dismiss';
}

// Status → dashicon + CSS modifier + screen-reader label (label localized server-side).
$bsc_status_meta = function ( $status ) use ( $SL, $lbl_passed, $lbl_warn, $lbl_action ) {
	switch ( $status ) {
		case 'pass':
			return array( 'icon' => 'dashicons-yes-alt', 'cls' => 'biscotti-badge-pass', 'sr' => isset( $SL['pass'] ) ? $SL['pass'] : $lbl_passed );
		case 'warning':
			return array( 'icon' => 'dashicons-warning', 'cls' => 'biscotti-badge-warning', 'sr' => isset( $SL['warning'] ) ? $SL['warning'] : $lbl_warn );
		case 'fail':
			return array( 'icon' => 'dashicons-dismiss', 'cls' => 'biscotti-badge-fail', 'sr' => isset( $SL['fail'] ) ? $SL['fail'] : $lbl_action );
		default:
			return array( 'icon' => 'dashicons-info', 'cls' => 'biscotti-badge-info', 'sr' => isset( $SL['info'] ) ? $SL['info'] : __( 'Reminder', 'biscotti-cmp' ) );
	}
};
?>

<div class="biscotti-page biscotti-page-compliance">

	<?php if ( $region_label || $model_label || $jur_name ) : ?>
		<div class="biscotti-compliance-header">
			<span class="biscotti-compliance-jur">
				<?php echo esc_html( trim( $region_emoji . ' ' . ( $jur_name ? $jur_name : $region_label ) ) ); ?>
			</span>
			<?php if ( $framework ) : ?>
				<span class="biscotti-pill biscotti-pill-gray"><?php echo esc_html( $framework ); ?></span>
			<?php endif; ?>
			<?php if ( $model_label ) : ?>
				<span class="biscotti-pill biscotti-pill-blue"><?php echo esc_html( $model_label ); ?></span>
			<?php endif; ?>
		</div>
	<?php endif; ?>

	<!-- Overall Compliance Score -->
	<div class="biscotti-stats-grid">
		<?php
		Biscotti_Template_Renderer::stat_card( $lbl_score, esc_html( $score . '%' ), $score_icon );
		if ( ! $no_requirement ) {
			Biscotti_Template_Renderer::stat_card( $lbl_passed, esc_html( (string) $pass_count ), 'dashicons-yes-alt' );
			Biscotti_Template_Renderer::stat_card( $lbl_warn, esc_html( (string) $warn_count ), 'dashicons-warning' );
			Biscotti_Template_Renderer::stat_card( $lbl_action, esc_html( (string) $fail_count ), 'dashicons-dismiss' );
		}
		?>
	</div>

	<?php if ( $no_requirement ) : ?>
		<div class="biscotti-empty-state biscotti-compliance-noreq">
			<span class="dashicons dashicons-yes-alt"></span>
			<p><strong><?php echo esc_html( $data['noRequirementText'] ?? '' ); ?></strong></p>
			<p class="description"><?php echo esc_html( $data['noRequirementDesc'] ?? '' ); ?></p>
		</div>
	<?php elseif ( ! empty( $checks ) ) : ?>
		<h2><?php echo esc_html( $lbl_checks ); ?></h2>
		<table class="widefat biscotti-data-table biscotti-compliance-table">
			<thead>
				<tr>
					<th scope="col" class="biscotti-col-status"><?php esc_html_e( 'Status', 'biscotti-cmp' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Check', 'biscotti-cmp' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Description', 'biscotti-cmp' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php
				foreach ( $checks as $check ) :
					$c_status = isset( $check['status'] ) ? (string) $check['status'] : 'info';
					$c_name   = isset( $check['name'] ) ? (string) $check['name'] : '';
					$c_desc   = isset( $check['description'] ) ? (string) $check['description'] : '';
					$meta     = $bsc_status_meta( $c_status );
					?>
					<tr class="biscotti-check-row biscotti-check-<?php echo esc_attr( $c_status ); ?>">
						<td class="biscotti-col-status">
							<span class="dashicons <?php echo esc_attr( $meta['icon'] . ' ' . $meta['cls'] ); ?>"></span>
							<span class="screen-reader-text"><?php echo esc_html( $meta['sr'] ); ?></span>
						</td>
						<td class="biscotti-check-name"><?php echo esc_html( $c_name ); ?></td>
						<td class="biscotti-check-desc"><?php echo esc_html( $c_desc ); ?></td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>

		<?php
		// Recommendations for checks that need attention (fail/warning, never reminders).
		$action_items = array_filter(
			$checks,
			function ( $check ) {
				$s = isset( $check['status'] ) ? $check['status'] : '';
				return ( 'fail' === $s || 'warning' === $s ) && ! empty( $check['description'] );
			}
		);
		if ( ! empty( $action_items ) ) :
			?>
			<h2><?php esc_html_e( 'Recommendations', 'biscotti-cmp' ); ?></h2>
			<div class="biscotti-recommendations">
				<?php foreach ( $action_items as $check ) : ?>
					<div class="biscotti-recommendation-card biscotti-rec-<?php echo esc_attr( $check['status'] ); ?>">
						<strong><?php echo esc_html( $check['name'] ?? '' ); ?></strong>
						<p><?php echo esc_html( $check['description'] ?? '' ); ?></p>
					</div>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>

	<?php else : ?>
		<p><?php esc_html_e( 'No compliance checks available. Please ensure your site is connected to the Biscotti Cloud API.', 'biscotti-cmp' ); ?></p>
	<?php endif; ?>

</div>
<?php
