<?php
/**
 * Reports admin controller.
 *
 * @package Awesomemotive\Rewardswp\Controllers\Admin
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Controllers\Admin;

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

use Awesomemotive\Rewardswp\Interfaces\ControllerInterface;
use Awesomemotive\Rewardswp\Factories\ViewsFactory;
use Awesomemotive\Rewardswp\Services\AssetsService;
use Awesomemotive\Rewardswp\Services\DashboardService;
use Awesomemotive\Rewardswp\Helpers\IconHelper;
use Awesomemotive\Rewardswp\Controllers\Admin\MainAdminController;
use Awesomemotive\Rewardswp\Views\Admin\ReportsView;
use function Awesomemotive\Rewardswp\Helpers\can_access_rewardswp_feature;

class ReportsAdminController implements ControllerInterface {
	const MENU_SLUG     = 'rewardswp-reports';
	const MENU_POSITION = 59;

	/**
	 * Views factory instance.
	 *
	 * @since 1.0.0
	 *
	 * @var ViewsFactory
	 */
	private ViewsFactory $views_factory;

	/**
	 * Assets service instance.
	 *
	 * @since 1.0.0
	 *
	 * @var AssetsService
	 */
	private AssetsService $assets_service;

	/**
	 * Icon helper instance.
	 *
	 * @since 1.0.0
	 *
	 * @var IconHelper
	 */
	private IconHelper $icon_helper;

	/**
	 * Dashboard service instance.
	 *
	 * @since 1.0.0
	 *
	 * @var DashboardService
	 */
	private DashboardService $dashboard_service;

	public function __construct( ViewsFactory $views_factory, AssetsService $assets_service, IconHelper $icon_helper, DashboardService $dashboard_service ) {
		$this->views_factory     = $views_factory;
		$this->assets_service    = $assets_service;
		$this->icon_helper       = $icon_helper;
		$this->dashboard_service = $dashboard_service;
	}

	public function get_page_title(): string {
		return esc_html__( 'Reports', 'rewardswp' );
	}

	public function init() {
		add_action( 'admin_menu', [ $this, 'add_admin_menu' ], self::MENU_POSITION );
		// Priority 20 ensures script is registered (priority 10) before we localize.
		add_action( 'admin_enqueue_scripts', [ $this, 'localize_export_drawer_config' ], 20 );
	}

	public function add_admin_menu(): void {
		add_submenu_page(
			MainAdminController::MAIN_MENU_SLUG,
			$this->get_page_title(),
			$this->get_page_title(),
			'manage_options',
			self::MENU_SLUG,
			[ $this, 'render_page' ],
			self::MENU_POSITION
		);
	}

	public function render_page(): void {
		$view = $this->views_factory->get_view( ReportsView::class );
		$view->render();
	}

	/**
	 * Localize export drawer configuration for JavaScript.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	public function localize_export_drawer_config(): void {
		// Only load on our Reports page.
		$screen = get_current_screen();
		if ( ! $screen || false === strpos( $screen->id, self::MENU_SLUG ) ) {
			return;
		}

		$can_export = can_access_rewardswp_feature( 'exports' );

		$config = [
			'ajaxUrl'   => admin_url( 'admin-ajax.php' ),
			'nonce'     => wp_create_nonce( 'rewardswp-exports-nonce' ),
			'canExport' => $can_export,
			'labels'    => [
				'subtitle'        => __( 'Export to CSV', 'rewardswp' ),
				'addTitle'        => __( 'Start Export', 'rewardswp' ),
				'exportStarted'   => __( 'Export started! Check the Export History for progress.', 'rewardswp' ),
				'failedToStart'   => __( 'Failed to start export', 'rewardswp' ),
				'errorOccurred'   => __( 'An error occurred', 'rewardswp' ),
				'exportRestarted' => __( 'Export restarted', 'rewardswp' ),
				'failedToRetry'   => __( 'Failed to retry export', 'rewardswp' ),
				'rangeLabels'     => [
					'last_7_days'    => __( 'Last 7 days', 'rewardswp' ),
					'last_14_days'   => __( 'Last 14 days', 'rewardswp' ),
					'last_30_days'   => __( 'Last 30 days', 'rewardswp' ),
					'last_60_days'   => __( 'Last 60 days', 'rewardswp' ),
					'last_90_days'   => __( 'Last 90 days', 'rewardswp' ),
					'last_12_months' => __( 'Last 12 months', 'rewardswp' ),
					'all_time'       => __( 'All time', 'rewardswp' ),
				],
				'typeLabels'      => [
					'members'   => __( 'Members', 'rewardswp' ),
					'points'    => __( 'Points Activity', 'rewardswp' ),
					'referrals' => __( 'Referrals', 'rewardswp' ),
					'rewards'   => __( 'Rewards', 'rewardswp' ),
				],
				'statusLabels'    => [
					'completed'  => __( 'Ready', 'rewardswp' ),
					'processing' => __( 'Processing', 'rewardswp' ),
					'pending'    => __( 'Pending', 'rewardswp' ),
					'failed'     => __( 'Failed', 'rewardswp' ),
				],
			],
		];

		wp_localize_script(
			AssetsService::ADMIN_SCRIPT_HANDLE,
			'rewardswpExportDrawerConfig',
			$config
		);
	}
}
