<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class BingoCR_Admin {

	public function register(): void {
		add_action( 'admin_menu',            array( $this, 'add_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	public function add_menu(): void {
		add_menu_page(
			__( 'BingoCR Car Rental', 'bingocar' ),
			__( 'BingoCR', 'bingocar' ),
			'manage_options',
			'bingocr',
			array( $this, 'page_dashboard' ),
			'dashicons-car',
			30
		);
		add_submenu_page( 'bingocr', __( 'Dashboard', 'bingocar' ),    __( 'Dashboard', 'bingocar' ),    'manage_options', 'bingocr',                  array( $this, 'page_dashboard' ) );
		add_submenu_page( 'bingocr', __( 'Fleet', 'bingocar' ),        __( 'Fleet', 'bingocar' ),        'manage_options', 'bingocr-cars',             array( $this, 'page_cars' ) );
		add_submenu_page( 'bingocr', __( 'Reservations', 'bingocar' ), __( 'Reservations', 'bingocar' ), 'manage_options', 'bingocr-reservations',     array( $this, 'page_reservations' ) );
		add_submenu_page( 'bingocr', __( 'Locations', 'bingocar' ),    __( 'Locations', 'bingocar' ),    'manage_options', 'bingocr-locations',        array( $this, 'page_locations' ) );
		add_submenu_page( 'bingocr', __( 'Extras', 'bingocar' ),       __( 'Extras', 'bingocar' ),       'manage_options', 'bingocr-extras',           array( $this, 'page_extras' ) );
		add_submenu_page( 'bingocr', __( 'Pricing', 'bingocar' ),      __( 'Pricing', 'bingocar' ),      'manage_options', 'bingocr-pricing-rules',    array( $this, 'page_pricing' ) );
		add_submenu_page( 'bingocr', __( 'Payment', 'bingocar' ),      __( 'Payment', 'bingocar' ),      'manage_options', 'bingocr-payment',          array( $this, 'page_payment' ) );
		add_submenu_page( 'bingocr', __( 'Settings', 'bingocar' ),     __( 'Settings', 'bingocar' ),     'manage_options', 'bingocr-settings',         array( $this, 'page_settings' ) );
	}

	public function enqueue_assets( string $hook ): void {
		$pages = array(
			'toplevel_page_bingocr',
			'bingocr_page_bingocr-cars',
			'bingocr_page_bingocr-reservations',
			'bingocr_page_bingocr-locations',
			'bingocr_page_bingocr-extras',
			'bingocr_page_bingocr-pricing-rules',
			'bingocr_page_bingocr-payment',
			'bingocr_page_bingocr-settings',
		);

		if ( ! in_array( $hook, $pages, true ) ) {
			return;
		}

		wp_enqueue_media();

		wp_enqueue_style(
			'bingocr-admin',
			BINGOCR_URL . 'admin/css/bingocr-admin.css',
			array( 'wp-admin' ),
			BINGOCR_VERSION
		);

		wp_enqueue_script(
			'bingocr-admin',
			BINGOCR_URL . 'admin/js/bingocr-admin.js',
			array( 'wp-api-fetch', 'wp-i18n' ),
			BINGOCR_VERSION,
			true
		);

		wp_localize_script( 'bingocr-admin', 'bingocrAdmin', array(
			'restUrl'  => esc_url_raw( rest_url( 'bingocr/v1/' ) ),
			'nonce'    => wp_create_nonce( 'wp_rest' ),
			// Redacted: never expose the PayPal client secret in page source / JS.
			'settings' => BingoCR_Crypto::redact_settings( BingoCR_DB::get_settings() ),
			'i18n'     => array(
				'confirm_delete'  => __( 'Are you sure you want to delete this item?', 'bingocar' ),
				'saved'           => __( 'Saved successfully.', 'bingocar' ),
				'error'           => __( 'An error occurred. Please try again.', 'bingocar' ),
				'select_image'    => __( 'Select Image', 'bingocar' ),
				'use_image'       => __( 'Use This Image', 'bingocar' ),
				'upgrade_pro'     => __( 'Upgrade to Pro →', 'bingocar' ),
				// Shared table/button strings.
				'edit'            => __( 'Edit', 'bingocar' ),
				'delete'          => __( 'Delete', 'bingocar' ),
				'view'            => __( 'View', 'bingocar' ),
				'actions'         => __( 'Actions', 'bingocar' ),
				'status'          => __( 'Status', 'bingocar' ),
				'name'            => __( 'Name', 'bingocar' ),
				'type'            => __( 'Type', 'bingocar' ),
				'price'           => __( 'Price', 'bingocar' ),
				'value'           => __( 'Value', 'bingocar' ),
				'priority'        => __( 'Priority', 'bingocar' ),
				'applies_to'      => __( 'Applies To', 'bingocar' ),
				'all_cars'        => __( 'All Cars', 'bingocar' ),
				'category'        => __( 'Category', 'bingocar' ),
				'category_prefix' => __( 'Category:', 'bingocar' ),
				'car_hash'        => __( 'Car #', 'bingocar' ),
				'days'            => __( 'days', 'bingocar' ),
				'per_day'         => __( '/day', 'bingocar' ),
				// Fleet page.
				'no_cars'         => __( 'No cars yet. Add your first car.', 'bingocar' ),
				'photo'           => __( 'Photo', 'bingocar' ),
				'vehicle'         => __( 'Vehicle', 'bingocar' ),
				'price_day'       => __( 'Price/Day', 'bingocar' ),
				'qty'             => __( 'Qty', 'bingocar' ),
				'edit_car'        => __( 'Edit Car', 'bingocar' ),
				'add_car'         => __( 'Add Car', 'bingocar' ),
				'global_default'  => __( 'Global default:', 'bingocar' ),
				'pct_hint'        => __( 'e.g. 20 = 20% of rental total', 'bingocar' ),
				'flat_hint'       => __( 'Flat deposit amount', 'bingocar' ),
				'from_day'        => __( 'From day', 'bingocar' ),
				'to_day'          => __( 'To day', 'bingocar' ),
				'remove_tier'     => __( 'Remove tier', 'bingocar' ),
				// Reservations page.
				'ref'             => __( 'Ref', 'bingocar' ),
				'reference'       => __( 'Reference', 'bingocar' ),
				'customer'        => __( 'Customer', 'bingocar' ),
				'pickup'          => __( 'Pickup', 'bingocar' ),
				'return'          => __( 'Return', 'bingocar' ),
				'total'           => __( 'Total', 'bingocar' ),
				'paid'            => __( 'Paid', 'bingocar' ),
				'no_reservations' => __( 'No reservations found.', 'bingocar' ),
				// Reservation payment details.
				'payment_details' => __( 'Payment Details', 'bingocar' ),
				'pay_method'      => __( 'Method', 'bingocar' ),
				'pay_status'      => __( 'Payment status', 'bingocar' ),
				'pay_amount'      => __( 'Amount paid', 'bingocar' ),
				'pay_fee'         => __( 'PayPal fee', 'bingocar' ),
				'pay_net'         => __( 'Net received', 'bingocar' ),
				'pay_txn'         => __( 'Transaction ID', 'bingocar' ),
				'pay_order'       => __( 'Order ID', 'bingocar' ),
				'pay_payer'       => __( 'Payer email', 'bingocar' ),
				'pay_date'        => __( 'Paid on', 'bingocar' ),
				'pay_cash'        => __( 'Cash on pickup', 'bingocar' ),
				'pay_card'        => __( 'Card on pickup', 'bingocar' ),
				'pay_bank'        => __( 'Bank transfer', 'bingocar' ),
				'pay_paypal'      => __( 'PayPal', 'bingocar' ),
				// Locations page.
				'city'            => __( 'City', 'bingocar' ),
				'phone'           => __( 'Phone', 'bingocar' ),
				'hours'           => __( 'Hours', 'bingocar' ),
				'edit_location'   => __( 'Edit Location', 'bingocar' ),
				'add_location'    => __( 'Add Location', 'bingocar' ),
				// Extras page.
				'edit_extra'      => __( 'Edit Extra', 'bingocar' ),
				'add_extra'       => __( 'Add Extra', 'bingocar' ),
				// Pricing page — seasonal.
				'no_seasonal'     => __( 'No seasonal pricing rules yet.', 'bingocar' ),
				'period'          => __( 'Period', 'bingocar' ),
				'modifier'        => __( 'Modifier', 'bingocar' ),
				'edit_season'     => __( 'Edit Season', 'bingocar' ),
				'add_season'      => __( 'Add Season', 'bingocar' ),
				// Pricing page — duration rules.
				'no_period'       => __( 'No duration-based pricing rules yet.', 'bingocar' ),
				'day_range'       => __( 'Day Range', 'bingocar' ),
				'fixed_total'     => __( 'Fixed Total', 'bingocar' ),
				'rate_day'        => __( 'Rate/Day', 'bingocar' ),
				'edit_period'     => __( 'Edit Duration Rule', 'bingocar' ),
				'add_period'      => __( 'Add Duration Rule', 'bingocar' ),
				// Pricing page — discounts.
				'no_discounts'    => __( 'No discount codes yet.', 'bingocar' ),
				'code'            => __( 'Code', 'bingocar' ),
				'min_days'        => __( 'Min Days', 'bingocar' ),
				'uses'            => __( 'Uses', 'bingocar' ),
				'expires'         => __( 'Expires', 'bingocar' ),
				'edit_discount'   => __( 'Edit Discount Code', 'bingocar' ),
				'add_discount'    => __( 'Add Discount Code', 'bingocar' ),
			),
		) );
	}

	// ── Pages ────────────────────────────────────────────────────────────

	public function page_dashboard(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'bingocar' ) );
		}
		$this->load_template( 'page-dashboard' );
	}

	public function page_cars(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'bingocar' ) );
		}
		$this->load_template( 'page-cars' );
	}

	public function page_reservations(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'bingocar' ) );
		}
		$this->load_template( 'page-reservations' );
	}

	public function page_locations(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'bingocar' ) );
		}
		$this->load_template( 'page-locations' );
	}

	public function page_extras(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'bingocar' ) );
		}
		$this->load_template( 'page-extras' );
	}

	public function page_pricing(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'bingocar' ) );
		}
		$this->load_template( 'page-pricing' );
	}

	public function page_payment(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'bingocar' ) );
		}
		$this->load_template( 'page-payment' );
	}

	public function page_settings(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'bingocar' ) );
		}
		$this->load_template( 'page-settings' );
	}

	private function load_template( string $name ): void {
		$file = BINGOCR_DIR . 'templates/admin/' . $name . '.php';
		if ( file_exists( $file ) ) {
			include $file;
		}
	}
}
