<?php
/**
 * Country routing add/edit form renderer.
 *
 * @since   1.0.0
 * @package DonationRouter
 */

declare( strict_types=1 );
namespace DonationRouter;

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

/**
 * Renders the form for adding and editing country routing rules.
 *
 * Form submissions are posted to admin-post.php and handled by
 * Country_Routing_Handler. The form_id and account_id selects are
 * populated via AJAX on page load to avoid large inline option lists.
 *
 * @since 1.0.0
 */
class Country_Routing_Form {

	use Template_Loader;

	/**
	 * Routing data source.
	 *
	 * @since 1.0.0
	 *
	 * @var Country_Routing_Repository_Interface
	 */
	private Country_Routing_Repository_Interface $routing_repo;

	/**
	 * Account data source (for label lookup in edit mode).
	 *
	 * @since 1.0.0
	 *
	 * @var Account_Repository_Interface
	 */
	private Account_Repository_Interface $account_repo;

	/**
	 * Initialise the form renderer with required data sources.
	 *
	 * @since 1.0.0
	 *
	 * @param Country_Routing_Repository_Interface $routing_repo  Routing data source.
	 * @param Account_Repository_Interface         $account_repo  Account data source.
	 */
	public function __construct(
		Country_Routing_Repository_Interface $routing_repo,
		Account_Repository_Interface $account_repo
	) {
		$this->routing_repo = $routing_repo;
		$this->account_repo = $account_repo;
	}

	/**
	 * Render the add or edit form.
	 *
	 * When $routing_id is null the form renders in "add" mode with empty defaults.
	 * When an ID is supplied the form pre-fills with existing data.
	 *
	 * @since 1.0.0
	 *
	 * @param int|null $routing_id Existing routing rule ID, or null for new.
	 *
	 * @return void
	 */
	public function render( ?int $routing_id ): void {
		$routing  = null;
		$is_edit  = false;
		$back_url = add_query_arg(
			array(
				'post_type' => 'give_forms',
				'page'      => Admin_Settings::MENU_SLUG,
				'tab'       => Admin_Settings::TAB_ROUTING,
			),
			admin_url( 'edit.php' )
		);

		if ( null !== $routing_id && $routing_id > 0 ) {
			try {
				$routing = $this->routing_repo->find_by_id( $routing_id );
				$is_edit = true;
			} catch ( Country_Routing_Not_Found_Exception $e ) {
				echo '<div class="notice notice-error"><p>' . esc_html__( 'Routing rule not found.', 'alamin-donation-router' ) . '</p></div>';
				return;
			}
		}

		$heading = $is_edit
		? __( 'Edit Country Routing Rule', 'alamin-donation-router' )
		: __( 'Add Country Routing Rule', 'alamin-donation-router' );

		$country_code        = $routing ? $routing->country_code : '';
		$form_id             = $routing ? $routing->form_id : 0;
		$account_id          = $routing ? $routing->account_id : 0;
		$display_label       = $routing ? $routing->display_label : '';
		$display_description = $routing ? (string) $routing->display_description : '';
		$sort_order          = $routing ? $routing->sort_order : 0;
		$is_active           = $routing ? $routing->is_active : true;

		$action       = 'ador_save_routing';
		$nonce_action = $is_edit ? 'ador_save_routing_' . $routing->id : 'ador_save_routing_new';

		// Resolve account label for the select option before passing to template.
		$account_select_label = '';
		if ( $account_id > 0 ) {
			try {
				$account_select_label = $this->account_repo->find_by_id( $account_id )->account_label;
			} catch ( Account_Not_Found_Exception $e ) {
				$account_select_label = sprintf( '#%d', $account_id );
			}
		}

		$countries = Account_Form::get_paypal_countries();

		$this->render_template(
			'admin/country-routing-form.php',
			compact(
				'heading',
				'back_url',
				'is_edit',
				'routing',
				'country_code',
				'form_id',
				'account_id',
				'display_label',
				'display_description',
				'sort_order',
				'is_active',
				'action',
				'nonce_action',
				'account_select_label',
				'countries'
			)
		);
	}
}
