<?php
/**
 * Report Controller for Warranty Cart Admin REST API
 *
 * Handles reports and dashboard REST API operations.
 *
 * @package WarrantyCart
 */

namespace WarrantyCart\REST\Controllers;

use WarrantyCart\REST\BaseController;
use WP_REST_Server;

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

/**
 * ReportController class
 */
class ReportController extends BaseController {

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->rest_base = 'admin';
	}

	/**
	 * Register routes
	 */
	public function register_routes(): void {
		// Reports endpoints
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/reports',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_reports' ),
					'permission_callback' => array( $this, 'check_admin_permission' ),
					'args'                => $this->get_reports_args(),
				),
			)
		);

		// Reports export endpoint
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/reports/export',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'export_reports_csv' ),
					'permission_callback' => array( $this, 'check_admin_permission' ),
					'args'                => array(
						'date_range' => array(
							'required'          => false,
							'type'              => 'string',
							'default'           => '30',
							'enum'              => array( '7', '30', '90', '365' ),
							'description'       => __( 'Date range in days.', 'warranty-cart' ),
							'sanitize_callback' => 'sanitize_text_field',
						),
					),
				),
			)
		);

		// Dashboard data endpoint
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/dashboard',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_dashboard' ),
					'permission_callback' => array( $this, 'check_admin_permission' ),
					'args'                => array(
						'date_range' => array(
							'required'          => false,
							'type'              => 'integer',
							'default'           => 30,
							'description'       => __( 'Date range in days for sales data.', 'warranty-cart' ),
							'sanitize_callback' => 'absint',
							'validate_callback' => 'rest_validate_request_arg',
							'minimum'           => 1,
							'maximum'           => 365,
						),
					),
				),
			)
		);

		// Dashboard export endpoint
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/dashboard/export',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'export_dashboard_csv' ),
					'permission_callback' => array( $this, 'check_admin_permission' ),
					'args'                => array(
						'date_range' => array(
							'required'          => false,
							'type'              => 'integer',
							'default'           => 30,
							'description'       => __( 'Date range in days for sales data.', 'warranty-cart' ),
							'sanitize_callback' => 'absint',
							'validate_callback' => 'rest_validate_request_arg',
							'minimum'           => 1,
							'maximum'           => 365,
						),
					),
				),
			)
		);
	}

	/**
	 * Get reports arguments
	 */
	private function get_reports_args(): array {
		$args = array(
			'date_range' => array(
				'required'          => false,
				'type'              => 'string',
				'default'           => '30',
				'enum'              => array( '7', '30', '90', '365' ),
				'description'       => __( 'Date range in days.', 'warranty-cart' ),
				'sanitize_callback' => 'sanitize_text_field',
			),
		);

		/**
		 * Filter reports REST API arguments.
		 *
		 * @param array $args Arguments array.
		 */
		return apply_filters( 'warranty_cart_rest_reports_args', $args );
	}

	/**
	 * Get dashboard data
	 *
	 * Retrieves all data needed for the admin dashboard.
	 */
	public function get_dashboard( WP_REST_Request $request ): WP_REST_Response {
		$date_range = $request->get_param( 'date_range' );
		$date_range = $date_range ? absint( $date_range ) : 30;

		$dashboard_data = warranty_cart()->reports->get_dashboard_data( $date_range );

		// Add currency information
		$dashboard_data['currency']      = get_woocommerce_currency_symbol();
		$dashboard_data['currency_code'] = get_woocommerce_currency();

		return $this->success_response( $dashboard_data );
	}

	/**
	 * Get reports data
	 *
	 * Retrieves warranty sales analytics and reporting data.
	 */
	public function get_reports( WP_REST_Request $request ): WP_REST_Response {
		$date_range = (int) $request->get_param( 'date_range' );
		$reports    = warranty_cart()->reports->get_comprehensive_reports( $date_range );

		// Add currency information
		$reports['currency']      = get_woocommerce_currency_symbol();
		$reports['currency_code'] = get_woocommerce_currency();

		return $this->success_response( $reports );
	}

	/**
	 * Export reports CSV
	 *
	 * Exports comprehensive warranty reports data to CSV format.
	 */
	public function export_reports_csv( WP_REST_Request $request ) {
		$date_range = (int) $request->get_param( 'date_range' );

		// Call the export method from Reports class
		warranty_cart()->reports->export_reports_csv( $date_range );
	}

	/**
	 * Export dashboard CSV
	 *
	 * Exports dashboard summary data to CSV format.
	 */
	public function export_dashboard_csv( WP_REST_Request $request ) {
		$date_range = $request->get_param( 'date_range' );
		$date_range = $date_range ? absint( $date_range ) : 30;

		// Call the export method from Reports class
		warranty_cart()->reports->export_dashboard_csv( $date_range );
		// This method exits, so no return needed
	}
}
