<?php
defined( 'ABSPATH' ) || exit;

class Woocom_Pay_Emails {

	public static function init(): void {
		// Placeholder; emails sent directly via static methods.
	}

	private static function get_admin_email(): string {
		$settings = get_option( 'woocom_pay_settings', [] );
		return $settings['admin_email'] ?? get_option( 'admin_email' );
	}

	private static function email_enabled(): bool {
		$settings = get_option( 'woocom_pay_settings', [] );
		return ! empty( $settings['enable_emails'] );
	}

	private static function send( string $to, string $subject, string $body ): void {
		if ( ! self::email_enabled() ) {
			return;
		}
		$headers = [ 'Content-Type: text/html; charset=UTF-8' ];
		wp_mail( $to, $subject, $body, $headers );
	}

	private static function wrap( string $heading, string $content ): string {
		ob_start();
		?>
		<div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;padding:20px;">
			<div style="background:#2c3e50;padding:20px;text-align:center;border-radius:6px 6px 0 0;">
				<h1 style="color:#fff;margin:0;font-size:22px;"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></h1>
			</div>
			<div style="background:#fff;padding:30px;border:1px solid #ddd;border-top:none;border-radius:0 0 6px 6px;">
				<h2 style="color:#2c3e50;margin-top:0;"><?php echo esc_html( $heading ); ?></h2>
				<?php echo wp_kses_post( $content ); ?>
			</div>
			<p style="text-align:center;color:#999;font-size:12px;margin-top:10px;">
				Powered by Woocom-Pay &mdash; <?php echo esc_html( get_bloginfo( 'name' ) ); ?>
			</p>
		</div>
		<?php
		return ob_get_clean();
	}

	public static function send_customer_payment_submitted( WC_Order $order ): void {
		$to      = $order->get_billing_email();
		$subject = sprintf( __( '[%s] Payment Submitted – Order #%s', 'woocom-pay' ), get_bloginfo( 'name' ), $order->get_order_number() );
		$content = '<p>' . sprintf(
			/* translators: %s: order number */
			__( 'Thank you! Your payment for Order <strong>#%s</strong> has been submitted and is pending verification by our team.', 'woocom-pay' ),
			$order->get_order_number()
		) . '</p>';
		$content .= '<p>' . __( 'We will notify you once your payment is verified.', 'woocom-pay' ) . '</p>';
		self::send( $to, $subject, self::wrap( __( 'Payment Submitted', 'woocom-pay' ), $content ) );
	}

	public static function send_customer_payment_approved( WC_Order $order ): void {
		$to      = $order->get_billing_email();
		$subject = sprintf( __( '[%s] Payment Approved – Order #%s', 'woocom-pay' ), get_bloginfo( 'name' ), $order->get_order_number() );
		$content = '<p>' . sprintf(
			__( 'Great news! Your payment for Order <strong>#%s</strong> has been approved.', 'woocom-pay' ),
			$order->get_order_number()
		) . '</p>';

		$balance = (float) $order->get_meta( '_wcp_balance_amount' );
		if ( $balance > 0 ) {
			$link     = Woocom_Pay_Balance::get_balance_link( $order->get_id() );
			$content .= '<p>' . sprintf(
				__( 'Your remaining balance of <strong>%s</strong> is due. <a href="%s">Click here to pay the balance</a>.', 'woocom-pay' ),
				wc_price( $balance ),
				esc_url( $link )
			) . '</p>';
		}

		self::send( $to, $subject, self::wrap( __( 'Payment Approved', 'woocom-pay' ), $content ) );
	}

	public static function send_customer_payment_rejected( WC_Order $order, string $reason = '' ): void {
		$to      = $order->get_billing_email();
		$subject = sprintf( __( '[%s] Payment Rejected – Order #%s', 'woocom-pay' ), get_bloginfo( 'name' ), $order->get_order_number() );
		$content = '<p>' . sprintf(
			__( 'Unfortunately, your payment for Order <strong>#%s</strong> could not be verified.', 'woocom-pay' ),
			$order->get_order_number()
		) . '</p>';
		if ( $reason ) {
			$content .= '<p>' . __( 'Reason:', 'woocom-pay' ) . ' ' . esc_html( $reason ) . '</p>';
		}
		$content .= '<p>' . __( 'Please contact us for assistance.', 'woocom-pay' ) . '</p>';
		self::send( $to, $subject, self::wrap( __( 'Payment Rejected', 'woocom-pay' ), $content ) );
	}

	public static function send_customer_balance_reminder( WC_Order $order, string $link ): void {
		$to      = $order->get_billing_email();
		$balance = (float) $order->get_meta( '_wcp_balance_amount' );
		$subject = sprintf( __( '[%s] Balance Due – Order #%s', 'woocom-pay' ), get_bloginfo( 'name' ), $order->get_order_number() );
		$content = '<p>' . sprintf(
			__( 'You have a pending balance of <strong>%s</strong> for Order <strong>#%s</strong>.', 'woocom-pay' ),
			wc_price( $balance ),
			$order->get_order_number()
		) . '</p>';
		$content .= '<p><a href="' . esc_url( $link ) . '" style="background:#27ae60;color:#fff;padding:12px 24px;text-decoration:none;border-radius:4px;display:inline-block;">'
			. __( 'Pay Balance Now', 'woocom-pay' ) . '</a></p>';
		self::send( $to, $subject, self::wrap( __( 'Balance Due Reminder', 'woocom-pay' ), $content ) );
	}

	public static function send_admin_payment_submitted( WC_Order $order ): void {
		$to      = self::get_admin_email();
		$subject = sprintf( __( '[%s] New Payment Submission – Order #%s', 'woocom-pay' ), get_bloginfo( 'name' ), $order->get_order_number() );
		$verify_url = admin_url( 'admin.php?page=woocom-pay-verify&order_id=' . $order->get_id() );
		$content = '<p>' . sprintf(
			__( 'A new payment has been submitted for Order <strong>#%s</strong> by %s.', 'woocom-pay' ),
			$order->get_order_number(),
			esc_html( $order->get_formatted_billing_full_name() )
		) . '</p>';
		$content .= '<p><a href="' . esc_url( $verify_url ) . '">' . __( 'Verify Payment', 'woocom-pay' ) . '</a></p>';
		self::send( $to, $subject, self::wrap( __( 'Payment Verification Required', 'woocom-pay' ), $content ) );
	}

	public static function send_admin_balance_submitted( WC_Order $order ): void {
		$to      = self::get_admin_email();
		$subject = sprintf( __( '[%s] Balance Payment Submitted – Order #%s', 'woocom-pay' ), get_bloginfo( 'name' ), $order->get_order_number() );
		$verify_url = admin_url( 'admin.php?page=woocom-pay-verify&order_id=' . $order->get_id() );
		$content = '<p>' . sprintf(
			__( 'Balance payment submitted for Order <strong>#%s</strong>.', 'woocom-pay' ),
			$order->get_order_number()
		) . '</p>';
		$content .= '<p><a href="' . esc_url( $verify_url ) . '">' . __( 'Verify Balance Payment', 'woocom-pay' ) . '</a></p>';
		self::send( $to, $subject, self::wrap( __( 'Balance Payment Verification Required', 'woocom-pay' ), $content ) );
	}
}
