<?php
/**
 * Campaign Ending Notifications
 *
 * @package KNS_Dynamic_Discounts
 */

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

/**
 * Notifications for campaigns ending soon
 */
class KNSDD_Notification_Campaign_Ending implements KNSDD_INotificationType {

	/**
	 * Get notification type ID
	 *
	 * @return string
	 */
	public function get_id() {
		return 'campaign_ending';
	}

	/**
	 * Get priority
	 *
	 * @return int
	 */
	public function get_priority() {
		return 85;
	}

	/**
	 * Check if enabled
	 *
	 * @return bool
	 */
	public function is_enabled() {
		return apply_filters( 'knsdd_notification_campaign_ending_enabled', true );
	}

	/**
	 * Check and generate notifications
	 *
	 * @return array
	 */
	public function check() {
		$notifications = array();
		$groups = KNSDD_Data::get_groups();

		if ( empty( $groups ) || ! is_array( $groups ) ) {
			return $notifications;
		}

		$now = current_time( 'timestamp' );

		foreach ( $groups as $group_name ) {
			// Güvenlik: $group_name string olmalı
			if ( ! is_string( $group_name ) || empty( $group_name ) ) {
				continue;
			}
			
			// Get group dates
			$dates = KNSDD_Data::get_dates_for_group( $group_name );
			$end_date = isset( $dates['end'] ) ? $dates['end'] : '';

			if ( empty( $end_date ) ) {
				continue;
			}

			// End date should be interpreted as end of day (23:59:59)
			$end_timestamp = strtotime( $end_date . ' 23:59:59' );
			if ( ! $end_timestamp || $end_timestamp <= $now ) {
				continue;
			}

			$time_diff = $end_timestamp - $now;
			$hours_left = floor( $time_diff / 3600 );
			$days_left = floor( $time_diff / 86400 );

			// Check if campaign ends today (same day, but not yet expired)
			$today_start = strtotime( 'today 00:00:00' );
			$today_end = strtotime( 'today 23:59:59' );
			$ends_today = ( $end_timestamp >= $today_start && $end_timestamp <= $today_end && $end_timestamp > $now );

			// Today warning (Priority: 95)
			if ( $ends_today ) {
				$notifications[] = array(
					'id'       => 'campaign_ending_today_' . sanitize_key( $group_name ) . '_' . md5( $end_date ),
					'icon'     => '<span class="iconify" data-icon="mdi:fire"></span>',
					'message'  => sprintf(
						/* translators: %s: group name */
						__( '"%s" kampanyası bugün sona erecek', 'kns-dynamic-discounts-for-woocommerce' ),
						$group_name
					),
					'priority' => 95,
					'actions'  => array(
						array(
							'label' => __( 'Tarihi Uzat', 'kns-dynamic-discounts-for-woocommerce' ),
							'url'   => admin_url( 'admin.php?page=knsdd-groups&focus=' . urlencode( $group_name ) ),
							'type'  => 'primary',
						),
						array(
							'label' => __( 'Kuralları Yönet', 'kns-dynamic-discounts-for-woocommerce' ),
							'url'   => admin_url( 'admin.php?page=knsdd-rules&group=' . urlencode( $group_name ) ),
							'type'  => 'secondary',
						),
					),
					'meta'     => array(
						'group'      => $group_name,
						'hours_left' => $hours_left,
						'end_date'   => $end_date,
					),
				);
			}
			// 24 hours warning (Priority: 90)
			elseif ( $hours_left <= 24 && $hours_left > 0 ) {
				$notifications[] = array(
					'id'       => 'campaign_ending_24h_' . sanitize_key( $group_name ) . '_' . md5( $end_date ),
					'icon'     => '<span class="iconify" data-icon="mdi:clock-alert-outline"></span>',
					'message'  => sprintf(
						/* translators: %s: group name */
						__( '"%s" kampanyası 24 saat içinde sona erecek', 'kns-dynamic-discounts-for-woocommerce' ),
						$group_name
					),
					'priority' => 90,
					'actions'  => array(
						array(
							'label' => __( 'Tarihi Uzat', 'kns-dynamic-discounts-for-woocommerce' ),
							'url'   => admin_url( 'admin.php?page=knsdd-groups&focus=' . urlencode( $group_name ) ),
							'type'  => 'primary',
						),
						array(
							'label' => __( 'Kuralları Yönet', 'kns-dynamic-discounts-for-woocommerce' ),
							'url'   => admin_url( 'admin.php?page=knsdd-rules&group=' . urlencode( $group_name ) ),
							'type'  => 'secondary',
						),
					),
					'meta'     => array(
						'group'      => $group_name,
						'hours_left' => $hours_left,
						'end_date'   => $end_date,
					),
				);
			}
			// 3 days warning (Priority: 85)
			elseif ( $days_left <= 3 && $days_left > 1 ) {
				$notifications[] = array(
					'id'       => 'campaign_ending_3d_' . sanitize_key( $group_name ) . '_' . md5( $end_date ),
					'icon'     => '<span class="iconify" data-icon="mdi:clock-outline"></span>',
					'message'  => sprintf(
						/* translators: %s: group name */
						__( '"%s" kampanyası 3 gün içinde sona erecek', 'kns-dynamic-discounts-for-woocommerce' ),
						$group_name
					),
					'priority' => 85,
					'actions'  => array(
						array(
							'label' => __( 'Tarihi Uzat', 'kns-dynamic-discounts-for-woocommerce' ),
							'url'   => admin_url( 'admin.php?page=knsdd-groups&focus=' . urlencode( $group_name ) ),
							'type'  => 'primary',
						),
						array(
							'label' => __( 'Kuralları Yönet', 'kns-dynamic-discounts-for-woocommerce' ),
							'url'   => admin_url( 'admin.php?page=knsdd-rules&group=' . urlencode( $group_name ) ),
							'type'  => 'secondary',
						),
					),
					'meta'     => array(
						'group'     => $group_name,
						'days_left' => $days_left,
						'end_date'  => $end_date,
					),
				);
			}
		}

		return $notifications;
	}
}
