<?php
namespace Gigneo\Emails;

use Exception;
use WC_Email;

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

/**
 * Handles withdraw response email notifications.
 *
 * @since 0.0.1
 */
class Withdraw_Response extends WC_Email {
	/**
	 * Sets up email metadata, templates, placeholders, and recipient.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->id             = 'gigneo_withdraw_response';
		$this->title          = esc_html__( 'Gigneo - Withdraw response', 'gigneo' );
		$this->description    = esc_html__( 'This email is sent to the admin when a new withdraw response is submitted.', 'gigneo' );
		$this->template_html  = 'email/withdraw-response.php';
		$this->template_plain = 'email/plain/withdraw-response.php';
		$this->template_base  = GIGNEO_DIR_PATH . 'templates/';
		$this->customer_email = true;
		$this->placeholders   = array(
			'{withdrawal_id}'      => '123',
			'{withdrawal_status}'  => esc_html__( 'Approved', 'gigneo' ),
			'{withdrawal_amount}'  => gigneo_price_format( 0, true ),
			'{withdrawal_note}'    => 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quod.',
			'{withdrawal_date}'    => current_time( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ),
			'{withdrawal_updated}' => 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 ),
			'{profile_id}'         => 1,
			'{profile_name}'       => 'John Doe',
			'{moderator_name}'     => gigneo_moderator_name(),
		);

		parent::__construct();
	}

	/**
	 * Trigger the email when withdraw response 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_status       = get_post_status( $withdrawal_id );
			$withdrawal_status       = ( 'publish' === $withdrawal_status ) ? 'pending' : $withdrawal_status;
			$withdrawal_amount       = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_total' );
			$withdrawal_note         = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_withdrawal_note' );
			$withdrawal_publish_date = gigneo_datetime( get_post_time( 'Y-m-d H:i:s', true, $withdrawal_id ), $date_format );
			$withdrawal_updated_date = gigneo_datetime( get_post_modified_time( 'Y-m-d H:i:s', true, $withdrawal_id ), $date_format );
			$withdrawal_status_label = gigneo_status_label( $withdrawal_status );
			$profile_id              = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_profile_id' );
			$profile_name            = gigneo_profile_name( $profile_id );
			$profile_user_id         = get_post_field( 'post_author', $profile_id );
			$profile_user            = get_userdata( $profile_user_id );
			$payment_method          = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_payout' );
			$payout_fee              = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_commission' );
			$payout_fee              = $payout_fee['total'] ?? 0;
			$net_amount              = gigneo_post_meta( $withdrawal_id, '_gigneo_pm_subtotal' );
			$fee_label               = gigneo_option( '_gigneo_ts_withdrawal_fee_name', esc_html__( 'Withdraw Fee', 'gigneo' ) );
			$moderator_name          = gigneo_moderator_name();
			$payout_tax              = gigneo_calculate_tax( $profile_id, $net_amount );
			$payable_amount          = floatval( $net_amount - $payout_tax );

			if ( $profile_id ) {
				$this->recipient                            = $profile_user->user_email;
				$this->placeholders['{withdrawal_id}']      = $withdrawal_id;
				$this->placeholders['{withdrawal_status}']  = $withdrawal_status_label ?? '';
				$this->placeholders['{withdrawal_amount}']  = gigneo_price_format( $withdrawal_amount, true ) ?? '';
				$this->placeholders['{withdrawal_note}']    = $withdrawal_note ?? '';
				$this->placeholders['{withdrawal_date}']    = $withdrawal_publish_date ?? '';
				$this->placeholders['{withdrawal_updated}'] = $withdrawal_updated_date ?? '';
				$this->placeholders['{payment_method}']     = $payment_method['title'] ?? '';
				$this->placeholders['{payout_fee}']         = gigneo_price_format( $payout_fee, 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 ) ?? '';
				$this->placeholders['{profile_id}']         = $profile_id;
				$this->placeholders['{profile_name}']       = $profile_name ?? '';
				$this->placeholders['{moderator_name}']     = $moderator_name ?? '';
				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__( 'Withdrawal {withdrawal_status}', 'gigneo' );
	}

	/**
	 * Default email heading before settings override.
	 *
	 * @since 0.0.1
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return esc_html__( 'Withdrawal {withdrawal_status}', '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}'],
				'withdrawal_status'  => $this->placeholders['{withdrawal_status}'],
				'withdrawal_amount'  => $this->placeholders['{withdrawal_amount}'],
				'withdrawal_note'    => $this->placeholders['{withdrawal_note}'],
				'withdrawal_date'    => $this->placeholders['{withdrawal_date}'],
				'withdrawal_updated' => $this->placeholders['{withdrawal_updated}'],
				'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}'],
				'profile_id'         => $this->placeholders['{profile_id}'],
				'profile_name'       => $this->placeholders['{profile_name}'],
				'moderator_name'     => $this->placeholders['{moderator_name}'],
				'email_heading'      => $this->get_heading(),
				'freelancer_label'   => gigneo_freelancer_singular(),
				'sent_to_admin'      => false,
				'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}'],
				'withdrawal_status'  => $this->placeholders['{withdrawal_status}'],
				'withdrawal_amount'  => $this->placeholders['{withdrawal_amount}'],
				'withdrawal_date'    => $this->placeholders['{withdrawal_date}'],
				'withdrawal_note'    => $this->placeholders['{withdrawal_note}'],
				'withdrawal_updated' => $this->placeholders['{withdrawal_updated}'],
				'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}'],
				'profile_id'         => $this->placeholders['{profile_id}'],
				'profile_name'       => $this->placeholders['{profile_name}'],
				'moderator_name'     => $this->placeholders['{moderator_name}'],
				'email_heading'      => $this->get_heading(),
				'freelancer_label'   => gigneo_freelancer_singular(),
				'sent_to_admin'      => false,
				'plain_text'         => true,
				'email'              => $this,
			),
			'',
			$this->template_base
		);
	}
}
