<?php
/**
 * Fixed discount reward type implementation.
 *
 * @package Awesomemotive\Rewardswp\RewardTypes
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\RewardTypes;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractRewardType;
use Awesomemotive\Rewardswp\Factories\MailerFactory;
use Awesomemotive\Rewardswp\Factories\ViewsFactory;
use Awesomemotive\Rewardswp\Interfaces\IntegrationInterface;
use Awesomemotive\Rewardswp\Models\Reward;
use Awesomemotive\Rewardswp\Services\RewardsService;
use Awesomemotive\Rewardswp\Services\TrackingService;
use Awesomemotive\Rewardswp\Traits\CouponRewardFunctionalityTrait;

/**
 * Fixed Discount Reward Type
 *
 * Provides fixed amount discount coupons for advocates.
 * Uses CouponRewardFunctionalityTrait for all coupon-related functionality.
 *
 * @since 1.0.0
 */
class FixedDiscountRewardType extends AbstractRewardType {
	use CouponRewardFunctionalityTrait;

	/**
	 * Active integration instance.
	 *
	 * @since 1.0.0
	 *
	 * @var IntegrationInterface
	 */
	protected IntegrationInterface $integration;

	/**
	 * Mailer factory instance.
	 *
	 * @since 1.0.0
	 *
	 * @var MailerFactory
	 */
	protected MailerFactory $mailer_factory;

	/**
	 * Views factory instance.
	 *
	 * @since 1.0.0
	 *
	 * @var ViewsFactory
	 */
	protected ViewsFactory $views_factory;

	/**
	 * Rewards service instance.
	 *
	 * @since 1.0.0
	 *
	 * @var RewardsService
	 */
	protected RewardsService $rewards_service;

	/**
	 * Tracking service instance.
	 *
	 * @since 1.0.0
	 *
	 * @var TrackingService
	 */
	protected TrackingService $tracking_service;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param IntegrationInterface $integration Integration instance.
	 * @param MailerFactory        $mailer_factory Mailer factory instance.
	 * @param ViewsFactory         $views_factory Views factory instance.
	 * @param RewardsService       $rewards_service Rewards service instance.
	 * @param TrackingService      $tracking_service Tracking service instance.
	 */
	public function __construct(
		IntegrationInterface $integration,
		MailerFactory $mailer_factory,
		ViewsFactory $views_factory,
		RewardsService $rewards_service,
		TrackingService $tracking_service
	) {
		$this->integration      = $integration;
		$this->mailer_factory   = $mailer_factory;
		$this->views_factory    = $views_factory;
		$this->rewards_service  = $rewards_service;
		$this->tracking_service = $tracking_service;
	}

	/**
	 * Get reward type slug.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_type(): string {
		return 'fixed_discount';
	}

	/**
	 * Get admin title.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_admin_title(): string {
		return __( 'Amount discount', 'rewardswp' );
	}

	/**
	 * Get admin title for advocate.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_admin_advocate_title(): string {
		return __( 'Reward Advocates with an Amount Discount', 'rewardswp' );
	}

	/**
	 * Get admin title for friend.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_admin_friend_title(): string {
		return __( 'Reward Friends with an Amount Discount', 'rewardswp' );
	}

	/**
	 * Get panel title.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_panel_title(): string {
		if ( is_a( $this->reward, Reward::class ) ) {
			// The formatted amount already contains the full readable label (e.g., "$5 off coupon").
			return $this->get_reward_formatted_amount();
		}

		return __( 'Discount coupon', 'rewardswp' );
	}

	/**
	 * Get panel instructions.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_panel_instructions(): string {
		return __( 'Use this coupon on your next order!', 'rewardswp' );
	}

	/**
	 * Get icon name.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_icon_name(): string {
		return 'reward-amount-coupon';
	}

	/**
	 * Get default configuration.
	 *
	 * @since 1.0.0
	 *
	 * @return int[]
	 */
	public function get_defaults(): array {
		return [
			'reward_amount'        => 5,
			'minimum_order_amount' => 0,
			'reward_expiration'    => '+3 months',
		];
	}

	/**
	 * Get supported integrations.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function available_for(): array {
		return [
			'woocommerce',
			'easy_digital_downloads',
		];
	}

	/**
	 * Retrieve the reward formatted amount.
	 *
	 * To use a custom currency symbol, call set_currency_symbol() before this method.
	 *
	 * @since 1.0.0
	 *
	 * @param float  $amount Optional. Reward amount to format. Default 0 (uses attached reward amount when available).
	 * @param string $format Optional. Format identifier. Accepts 'default' (includes coupon suffix) or 'value_only'. Default 'default'.
	 *
	 * @return string
	 */
	public function get_reward_formatted_amount( float $amount = 0, string $format = 'default' ): string {
		if ( empty( $amount ) && is_a( $this->reward, Reward::class ) ) {
			$amount = $this->reward->amount;
		}

		if ( empty( $amount ) ) {
			return '';
		}

		$formatted_amount = $this->format_reward_amount( $amount );

		switch ( $format ) {
			case 'value_only':
				return sprintf(
					/* translators: %s is the formatted reward amount including currency symbol, e.g. "$5" or "5 €". */
					__( '%s off', 'rewardswp' ),
					$formatted_amount
				);
			default:
				return sprintf(
					/* translators: %s is the formatted reward amount including currency symbol, e.g. "$5" or "5 €". */
					__( '%s off coupon', 'rewardswp' ),
					$formatted_amount
				);
		}
	}

	/**
	 * Issue a friend's reward.
	 *
	 * Lite version: Friend rewards are not available, returns null.
	 * Override in Pro version (ProFixedDiscountRewardType) to enable friend rewards functionality.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args Reward arguments.
	 *
	 * @return Reward|null
	 */
	protected function issue_friend_reward( array $args ): ?Reward {
		// Lite: Friend rewards not available.
		return null;
	}
}
