<?php
namespace Gigneo\Emails;

use Exception;
use WC_Email;

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

/**
 * Handles withdraw request email notifications.
 *
 * @since 0.0.1
 */
class Withdraw_Request extends WC_Email {
	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_withdraw_request';
		$this->title          = esc_html__( 'Gigneo - Withdraw request', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to the admin when a new withdraw request is submitted.', 'gigneo' );
		$this->template_html  = 'email/withdraw-request.php';
		$this->template_plain = 'email/plain/withdraw-request.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = false;
		$this->placeholders   = array(
			'{profile_id}'        => 22,
			'{profile_name}'      => 'John Doe',
			'{withdrawal_id}'     => '123',
			'{withdrawal_amount}' => gigneo_price_format( 0, true ),
			'{withdrawal_date}'   => current_time( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ),
			'{payment_method}'    => 'PayPal',
			'{payout_fee}'        => gigneo_price_format( 0, true ),
			'{fee_label}'         => esc_html__( 'Withdraw fee', 'gigneo' ),
			'{net_amount}'        => gigneo_price_format( 0, true ),
			'{payout_tax}'        => gigneo_price_format( 0, true ),
			'{payable_amount}'    => gigneo_price_format( 0, true ),
		);

		parent::__construct();

		// Set recipient to the admin email (configurable in settings)
		$this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );
	}

	/**
	 * Trigger the email when withdraw request is submitted.
	 *
	 * @param int $profile_id
	 * @param int $withdrawal_id
	 *
	 * @throws Exception
	 * @since 0.0.1
	 */
	public function trigger( $profile_id, $withdrawal_id ) {
		if ( $profile_id && $withdrawal_id ) {
			$date_format             = get_option( 'date_format' );
			$withdrawal_id_formatted = gigneo_format_id( $withdrawal_id, 'withdraw', false );
			$withdrawal_amount       = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_total' );
			$withdrawal_publish_date = gigneo_datetime( get_post_time( 'Y-m-d H:i:s', true, $withdrawal_id ), $date_format );
			$profile_id              = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_profile_id' );
			$profile_name            = gigneo_profile_name( $profile_id );
			$payment_method          = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_payout' );
			$payout_fee              = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_commission' );
			$net_amount              = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_subtotal' );
			$fee_label               = gigneo_option( '_gigneo_ts_withdrawal_fee_name' );
			$payout_tax              = gigneo_calculate_tax( $profile_id, $net_amount );
			$payable_amount          = floatval( $net_amount - $payout_tax );

			if ( $profile_id ) {
				// Set recipient to admin email
				$this->recipient                                 = get_option( 'admin_email' );
				$this->placeholders['{profile_id}']              = $profile_id ?? '';
				$this->placeholders['{profile_name}']            = $profile_name ?? '';
				$this->placeholders['{withdrawal_id}']           = $withdrawal_id ?? '';
				$this->placeholders['{withdrawal_id_formatted}'] = $withdrawal_id_formatted ?? '';
				$this->placeholders['{withdrawal_status}']       = $withdrawal_status_label ?? '';
				$this->placeholders['{withdrawal_amount}']       = gigneo_price_format( $withdrawal_amount, true ) ?? '';
				$this->placeholders['{withdrawal_date}']         = $withdrawal_publish_date ?? '';
				$this->placeholders['{payment_method}']          = $payment_method['title'] ?? '';

				$this->placeholders['{payout_fee}']     = gigneo_price_format( $payout_fee['total'] ?? 0, true );
				$this->placeholders['{fee_label}']      = $fee_label ?? '';
				$this->placeholders['{net_amount}']     = gigneo_price_format( $net_amount, true ) ?? '';
				$this->placeholders['{payout_tax}']     = gigneo_price_format( $payout_tax, true ) ?? '';
				$this->placeholders['{payable_amount}'] = gigneo_price_format( $payable_amount, true ) ?? '';

				if ( $this->is_enabled() && $this->get_recipient() ) {
					$this->send(
						$this->get_recipient(),
						$this->get_subject(),
						$this->get_content(),
						$this->get_headers(),
						$this->get_attachments()
					);
				}
			}
		}
	}

	/**
	 * Default email subject line before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_subject() {
		return esc_html__( 'New withdrawal request – {withdrawal_id_formatted}', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'Withdrawal request', 'gigneo' );
	}

	/**
	 * Renders the HTML email body from the template.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_content_html() {
		return wc_get_template_html(
			$this->template_html,
			array(
				'withdrawal_id'     => $this->placeholders['{withdrawal_id}'],
				'profile_id'        => $this->placeholders['{profile_id}'],
				'profile_name'      => $this->placeholders['{profile_name}'],
				'withdrawal_amount' => $this->placeholders['{withdrawal_amount}'],
				'withdrawal_date'   => $this->placeholders['{withdrawal_date}'],
				'payment_method'    => $this->placeholders['{payment_method}'],
				'payout_fee'        => $this->placeholders['{payout_fee}'],
				'fee_label'         => $this->placeholders['{fee_label}'],
				'net_amount'        => $this->placeholders['{net_amount}'],
				'payout_tax'        => $this->placeholders['{payout_tax}'],
				'payable_amount'    => $this->placeholders['{payable_amount}'],
				'email_heading'     => $this->get_heading(),
				'freelancer_label'  => gigneo_freelancer_singular(),
				'sent_to_admin'     => true,
				'plain_text'        => false,
				'email'             => $this,
			),
			'',
			$this->template_base
		);
	}

	/**
	 * Renders the plain-text email body from the template.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_content_plain() {
		return wc_get_template_html(
			$this->template_plain,
			array(
				'withdrawal_id'     => $this->placeholders['{withdrawal_id}'],
				'profile_id'        => $this->placeholders['{profile_id}'],
				'profile_name'      => $this->placeholders['{profile_name}'],
				'withdrawal_amount' => $this->placeholders['{withdrawal_amount}'],
				'withdrawal_date'   => $this->placeholders['{withdrawal_date}'],
				'payment_method'    => $this->placeholders['{payment_method}'],
				'payout_fee'        => $this->placeholders['{payout_fee}'],
				'fee_label'         => $this->placeholders['{fee_label}'],
				'net_amount'        => $this->placeholders['{net_amount}'],
				'payout_tax'        => $this->placeholders['{payout_tax}'],
				'payable_amount'    => $this->placeholders['{payable_amount}'],
				'email_heading'     => $this->get_heading(),
				'freelancer_label'  => gigneo_freelancer_singular(),
				'sent_to_admin'     => true,
				'plain_text'        => true,
				'email'             => $this,
			),
			'',
			$this->template_base
		);
	}
}
