<?php
/**
 * Traffic analysis card for the main analytics dashboard.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Traffic_Dashboard_Widget {

	public function __construct(
		private readonly Niwa_AI_Traffic_Repository $repository
	) {}

	public function register_hooks(): void {
		add_action( 'niwa_ai_analytics_dashboard_extra_sections', array( $this, 'render' ), 20, 2 );
	}

	public function render( $filters = array(), $dashboard = null ): void {
		$days = isset( $filters['days'] ) ? max( 1, min( 365, (int) $filters['days'] ) ) : 30;
		$data = $this->repository->get_dashboard_data( $days );
		$summary = $data['summary'] ?? array();

		echo '<div class="niwa-card"><div class="niwa-card__header">';
		echo '<h2 class="niwa-card__title">' . esc_html__( 'Traffic Analysis', 'niwa-ai' ) . '</h2>';
		echo '</div><div class="niwa-card__body"><div class="niwa-stats">';
		$this->stat( $dashboard, __( 'Sessions', 'niwa-ai' ), (string) (int) ( $summary['sessions'] ?? 0 ) );
		$this->stat( $dashboard, __( 'Visitors', 'niwa-ai' ), (string) (int) ( $summary['visitors'] ?? 0 ) );
		$this->stat( $dashboard, __( 'Pageviews', 'niwa-ai' ), (string) (int) ( $summary['pageviews'] ?? 0 ) );
		$this->stat( $dashboard, __( 'Orders', 'niwa-ai' ), (string) (int) ( $summary['orders'] ?? 0 ) );
		$this->stat( $dashboard, __( 'Revenue', 'niwa-ai' ), $this->money( $dashboard, (float) ( $summary['revenue'] ?? 0 ) ) );
		$this->stat( $dashboard, __( 'Session CVR', 'niwa-ai' ), number_format_i18n( (float) ( $summary['conversion_rate'] ?? 0 ), 2 ) . '%' );
		echo '</div>';
		$this->render_rows( __( 'Top Channels', 'niwa-ai' ), $data['channels'] ?? array() );
		$this->render_rows( __( 'Top Campaigns', 'niwa-ai' ), $data['campaigns'] ?? array() );
		$this->render_rows( __( 'Top Landing / Content Pages', 'niwa-ai' ), $data['pages'] ?? array() );
		echo '</div></div>';
	}

	private function render_rows( $title, $rows ): void {
		if ( empty( $rows ) ) {
			return;
		}
		echo '<h3 style="margin:18px 0 8px;font-size:14px;">' . esc_html( $title ) . '</h3>';
		echo '<table class="niwa-table"><thead><tr><th>' . esc_html__( 'Name', 'niwa-ai' ) . '</th><th>' . esc_html__( 'Sessions', 'niwa-ai' ) . '</th><th>' . esc_html__( 'Pageviews', 'niwa-ai' ) . '</th><th>' . esc_html__( 'Conversions', 'niwa-ai' ) . '</th><th>' . esc_html__( 'Revenue', 'niwa-ai' ) . '</th></tr></thead><tbody>';
		foreach ( array_slice( (array) $rows, 0, 8 ) as $row ) {
			echo '<tr><td>' . esc_html( $row['label'] ?? '-' ) . '</td><td>' . esc_html( (string) (int) ( $row['sessions'] ?? 0 ) ) . '</td><td>' . esc_html( (string) (int) ( $row['pageviews'] ?? 0 ) ) . '</td><td>' . esc_html( (string) (int) ( $row['conversions'] ?? 0 ) ) . '</td><td>' . esc_html( number_format_i18n( (float) ( $row['revenue'] ?? 0 ), 2 ) ) . '</td></tr>';
		}
		echo '</tbody></table>';
	}

	private function stat( $dashboard, string $label, string $value ): void {
		if ( is_object( $dashboard ) && method_exists( $dashboard, 'render_stat_card' ) ) {
			$dashboard->render_stat_card( $label, $value );
			return;
		}
		echo '<div class="niwa-stat"><div class="niwa-stat__label">' . esc_html( $label ) . '</div><div class="niwa-stat__value">' . esc_html( $value ) . '</div></div>';
	}

	private function money( $dashboard, float $amount ): string {
		if ( is_object( $dashboard ) && method_exists( $dashboard, 'format_money' ) ) {
			return (string) $dashboard->format_money( $amount );
		}
		return number_format_i18n( $amount, 2 );
	}
}
