<?php
/**
 * Account 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 PayPal account records.
 *
 * Form submissions are posted to admin-post.php and handled by
 * Onboarding_Handler. For new accounts the "Connect with PayPal" button
 * initiates the OAuth flow; for existing accounts "Save Changes" updates
 * the non-credential fields.
 *
 * @since 1.0.0
 */
class Account_Form {

	use Template_Loader;

	/**
	 * Account data source.
	 *
	 * @since 1.0.0
	 *
	 * @var Account_Repository_Interface
	 */
	private Account_Repository_Interface $account_repo;

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

	/**
	 * Render the add or edit form.
	 *
	 * When $account_id is null the form renders in "add" mode and includes the
	 * "Connect with PayPal" button. When an ID is supplied the form pre-fills
	 * with existing data and shows "Save Changes".
	 *
	 * @since 1.0.0
	 *
	 * @param int|null $account_id  Existing account ID, or null for new.
	 *
	 * @return void
	 */
	public function render( ?int $account_id ): void {
		$account  = null;
		$is_edit  = false;
		$back_url = add_query_arg(
			array(
				'post_type' => 'give_forms',
				'page'      => Admin_Settings::MENU_SLUG,
				'tab'       => Admin_Settings::TAB_ACCOUNTS,
			),
			admin_url( 'edit.php' )
		);

		if ( null !== $account_id && $account_id > 0 ) {
			try {
				$account = $this->account_repo->find_by_id( $account_id );
				$is_edit = true;
			} catch ( Account_Not_Found_Exception $e ) {
				echo '<div class="notice notice-error"><p>' . esc_html__( 'Account not found.', 'alamin-donation-router' ) . '</p></div>';
				return;
			}
		}

		$heading = $is_edit
			? __( 'Edit PayPal Account', 'alamin-donation-router' )
			: __( 'Add PayPal Account', 'alamin-donation-router' );

		// Field values — from existing account or empty defaults.
		$label           = $account ? $account->account_label : '';
		$country         = $account ? $account->account_country : 'US';
		$name            = $account ? $account->legal_entity_name : '';
		$tax_id          = $account ? $account->legal_entity_tax_id : '';
		$address         = $account ? $account->legal_entity_address : '';
		$countries       = self::get_paypal_countries();
		$receipt_current = $account
			? (string) get_option( Receipt_Customizer::OPTION_TEMPLATE_PREFIX . $account->id, '' )
			: '';

		$this->render_template(
			'admin/account-form.php',
			compact( 'heading', 'back_url', 'is_edit', 'account', 'label', 'country', 'name', 'tax_id', 'address', 'countries', 'receipt_current' )
		);
	}
	/**
	 * Return the list of PayPal-supported countries for Business accounts.
	 *
	 * @since 1.0.0
	 *
	 * @return array<string,string>  Country code => display name pairs.
	 */
	public static function get_paypal_countries(): array {
		return array(
			'AU' => __( 'Australia', 'alamin-donation-router' ),
			'AT' => __( 'Austria', 'alamin-donation-router' ),
			'BE' => __( 'Belgium', 'alamin-donation-router' ),
			'BR' => __( 'Brazil', 'alamin-donation-router' ),
			'BG' => __( 'Bulgaria', 'alamin-donation-router' ),
			'CA' => __( 'Canada', 'alamin-donation-router' ),
			'CL' => __( 'Chile', 'alamin-donation-router' ),
			'CN' => __( 'China', 'alamin-donation-router' ),
			'CO' => __( 'Colombia', 'alamin-donation-router' ),
			'CY' => __( 'Cyprus', 'alamin-donation-router' ),
			'CZ' => __( 'Czech Republic', 'alamin-donation-router' ),
			'DK' => __( 'Denmark', 'alamin-donation-router' ),
			'EE' => __( 'Estonia', 'alamin-donation-router' ),
			'FI' => __( 'Finland', 'alamin-donation-router' ),
			'FR' => __( 'France', 'alamin-donation-router' ),
			'DE' => __( 'Germany', 'alamin-donation-router' ),
			'GR' => __( 'Greece', 'alamin-donation-router' ),
			'HK' => __( 'Hong Kong', 'alamin-donation-router' ),
			'HU' => __( 'Hungary', 'alamin-donation-router' ),
			'IN' => __( 'India', 'alamin-donation-router' ),
			'IE' => __( 'Ireland', 'alamin-donation-router' ),
			'IL' => __( 'Israel', 'alamin-donation-router' ),
			'IT' => __( 'Italy', 'alamin-donation-router' ),
			'JP' => __( 'Japan', 'alamin-donation-router' ),
			'LV' => __( 'Latvia', 'alamin-donation-router' ),
			'LI' => __( 'Liechtenstein', 'alamin-donation-router' ),
			'LT' => __( 'Lithuania', 'alamin-donation-router' ),
			'LU' => __( 'Luxembourg', 'alamin-donation-router' ),
			'MY' => __( 'Malaysia', 'alamin-donation-router' ),
			'MT' => __( 'Malta', 'alamin-donation-router' ),
			'MX' => __( 'Mexico', 'alamin-donation-router' ),
			'NL' => __( 'Netherlands', 'alamin-donation-router' ),
			'NZ' => __( 'New Zealand', 'alamin-donation-router' ),
			'NO' => __( 'Norway', 'alamin-donation-router' ),
			'PH' => __( 'Philippines', 'alamin-donation-router' ),
			'PL' => __( 'Poland', 'alamin-donation-router' ),
			'PT' => __( 'Portugal', 'alamin-donation-router' ),
			'RO' => __( 'Romania', 'alamin-donation-router' ),
			'SG' => __( 'Singapore', 'alamin-donation-router' ),
			'SK' => __( 'Slovakia', 'alamin-donation-router' ),
			'SI' => __( 'Slovenia', 'alamin-donation-router' ),
			'ZA' => __( 'South Africa', 'alamin-donation-router' ),
			'ES' => __( 'Spain', 'alamin-donation-router' ),
			'SE' => __( 'Sweden', 'alamin-donation-router' ),
			'CH' => __( 'Switzerland', 'alamin-donation-router' ),
			'TH' => __( 'Thailand', 'alamin-donation-router' ),
			'TW' => __( 'Taiwan', 'alamin-donation-router' ),
			'TR' => __( 'Turkey', 'alamin-donation-router' ),
			'GB' => __( 'United Kingdom', 'alamin-donation-router' ),
			'US' => __( 'United States', 'alamin-donation-router' ),
		);
	}
}
