<?php
/**
 * Campaign Status Change Notifications
 *
 * @package KNS_Dynamic_Discounts
 */

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

/**
 * Notifications for campaign status changes
 */
class KNSDD_Notification_Campaign_Status implements KNSDD_INotificationType {

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

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

	/**
	 * Check if enabled
	 *
	 * @return bool
	 */
	public function is_enabled() {
		return apply_filters( 'knsdd_notification_campaign_status_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' );
		$today_start = strtotime( 'today', $now );
		$today_end = strtotime( 'tomorrow', $now ) - 1;

		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 );
			$start_date = isset( $dates['start'] ) ? $dates['start'] : '';
			$end_date = isset( $dates['end'] ) ? $dates['end'] : '';

			// Campaign started today (Priority: 70)
			if ( $start_date ) {
				$start_timestamp = strtotime( $start_date );
				if ( $start_timestamp >= $today_start && $start_timestamp <= $today_end ) {
					$notifications[] = array(
						'id'       => 'campaign_started_today_' . sanitize_key( $group_name ) . '_' . md5( $start_date ),
						'icon'     => '<span class="iconify" data-icon="mdi:check-circle"></span>',
						'message'  => sprintf(
							/* translators: %s: group name */
							__( '"%s" kampanyası bugün başladı', 'kns-dynamic-discounts-for-woocommerce' ),
							$group_name
						),
						'priority' => 70,
						'actions'  => array(
								array(
									'label' => __( 'Kural Yönetimi', 'kns-dynamic-discounts-for-woocommerce' ),
									'url'   => admin_url( 'admin.php?page=knsdd-rules&group=' . urlencode( $group_name ) ),
									'type'  => 'primary',
								),
							array(
								'label' => __( 'Görüntüle', 'kns-dynamic-discounts-for-woocommerce' ),
								'url'   => admin_url( 'admin.php?page=knsdd-rules&group=' . urlencode( $group_name ) ),
								'type'  => 'secondary',
							),
						),
						'meta'     => array(
							'group'      => $group_name,
							'start_date' => $start_date,
						),
					);
				}
			}

			// Campaign ended today (Priority: 75)
			// Only show if end date is in the past (before today)
			if ( $end_date ) {
				$end_timestamp = strtotime( $end_date . ' 23:59:59' );
				if ( $end_timestamp < $now && $end_timestamp >= $today_start ) {
					$notifications[] = array(
						'id'       => 'campaign_ended_today_' . sanitize_key( $group_name ) . '_' . md5( $end_date ),
						'icon'     => '<span class="iconify" data-icon="mdi:stop-circle"></span>',
						'message'  => sprintf(
							/* translators: %s: group name */
							__( '"%s" kampanyası bugün sona erdi', 'kns-dynamic-discounts-for-woocommerce' ),
							$group_name
						),
						'priority' => 75,
						'actions'  => array(
								array(
									'label' => __( 'Kural Yönetimi', 'kns-dynamic-discounts-for-woocommerce' ),
									'url'   => admin_url( 'admin.php?page=knsdd-rules&group=' . urlencode( $group_name ) ),
									'type'  => 'primary',
								),
						),
						'meta'     => array(
							'group'    => $group_name,
							'end_date' => $end_date,
						),
					);
				}
			}
		}

		return $notifications;
	}
}
