<?php
/**
 * Admin page renderer.
 *
 * @since 1.0.0
 */

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

class ZINV_Admin_Page {

	public static function register(): void {
		add_action( 'zoventic_invoices_render_admin_page', [ __CLASS__, 'render' ] );
		add_filter( 'admin_body_class', [ __CLASS__, 'add_admin_body_class' ] );
		add_action( 'in_admin_header', [ __CLASS__, 'suppress_admin_notices' ], 1 );
	}

	public static function add_admin_body_class( string $classes ): string {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( $screen && 'toplevel_page_zoventic-invoices' === $screen->id ) {
			return trim( $classes . ' zinv-fullscreen-app' );
		}
		return $classes;
	}

	public static function suppress_admin_notices(): void {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( $screen && 'toplevel_page_zoventic-invoices' === $screen->id ) {
			remove_all_actions( 'admin_notices' );
			remove_all_actions( 'all_admin_notices' );
			remove_all_actions( 'user_admin_notices' );
			remove_all_actions( 'network_admin_notices' );
		}
	}

	public static function render(): void {
		static $rendered = false;
		if ( $rendered ) {
			return;
		}
		$rendered = true;

		if ( ! current_user_can( 'zinv_view_dashboard' ) ) {
			wp_die(
				esc_html__( 'You do not have permission to access this page.', 'zoventic-invoices' ),
				esc_html__( 'Zoventic Invoices — Access Denied', 'zoventic-invoices' ),
				[ 'response' => 403 ]
			);
		}

		include ZINV_PLUGIN_DIR . 'templates/admin-page.php';
	}
}
