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

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

/**
 * Notifications for campaigns starting soon
 */
class KNSDD_Notification_Campaign_Starting implements KNSDD_INotificationType {

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

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

	/**
	 * Check if enabled
	 *
	 * @return bool
	 */
	public function is_enabled() {
		return apply_filters( 'knsdd_notification_campaign_starting_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 );
			$start_date = isset( $dates['start'] ) ? $dates['start'] : '';

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

			// Start date should be interpreted as start of day (00:00:00)
			$start_timestamp = strtotime( $start_date . ' 00:00:00' );
			if ( ! $start_timestamp || $start_timestamp <= $now ) {
				continue;
			}

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

			// Check if campaign starts tomorrow (next day, 00:00:00)
			$tomorrow_start = strtotime( 'tomorrow 00:00:00' );
			$tomorrow_end = strtotime( 'tomorrow 23:59:59' );
			$starts_tomorrow = ( $start_timestamp >= $tomorrow_start && $start_timestamp <= $tomorrow_end );

			// Tomorrow warning (Priority: 90)
			if ( $starts_tomorrow ) {
				$notifications[] = array(
					'id'       => 'campaign_starting_tomorrow_' . sanitize_key( $group_name ) . '_' . md5( $start_date ),
					'icon'     => '<span class="iconify" data-icon="mdi:rocket-launch"></span>',
					'message'  => sprintf(
						/* translators: %s: group name */
						__( '"%s" kampanyası yarın başlayacak', 'kns-dynamic-discounts-for-woocommerce' ),
						$group_name
					),
					'priority' => 90,
					'actions'  => array(
						array(
							'label' => __( 'Kuralları Kontrol Et', 'kns-dynamic-discounts-for-woocommerce' ),
							'url'   => admin_url( 'admin.php?page=knsdd-rules&group=' . urlencode( $group_name ) ),
							'type'  => 'primary',
						),
					),
					'meta'     => array(
						'group'      => $group_name,
						'start_date' => $start_date,
					),
				);
			}
			// 24 hours warning (Priority: 85)
			elseif ( $hours_left <= 24 && $hours_left > 0 ) {
				$notifications[] = array(
					'id'       => 'campaign_starting_24h_' . sanitize_key( $group_name ) . '_' . md5( $start_date ),
					'icon'     => '<span class="iconify" data-icon="mdi:calendar-clock"></span>',
					'message'  => sprintf(
						/* translators: %s: group name */
						__( '"%s" kampanyası 24 saat içinde başlayacak', 'kns-dynamic-discounts-for-woocommerce' ),
						$group_name
					),
					'priority' => 85,
					'actions'  => array(
						array(
							'label' => __( 'Tarihi Düzenle', 'kns-dynamic-discounts-for-woocommerce' ),
							'url'   => admin_url( 'admin.php?page=knsdd-groups&focus=' . urlencode( $group_name ) ),
							'type'  => 'primary',
						),
					),
					'meta'     => array(
						'group'       => $group_name,
						'hours_left'  => $hours_left,
						'start_date'  => $start_date,
					),
				);
			}
			// 3 days warning (Priority: 80)
			elseif ( $days_left <= 3 && $days_left > 1 ) {
				$notifications[] = array(
					'id'       => 'campaign_starting_3d_' . sanitize_key( $group_name ) . '_' . md5( $start_date ),
					'icon'     => '<span class="iconify" data-icon="mdi:calendar-start"></span>',
					'message'  => sprintf(
						/* translators: %s: group name */
						__( '"%s" kampanyası 3 gün içinde başlayacak', 'kns-dynamic-discounts-for-woocommerce' ),
						$group_name
					),
					'priority' => 80,
					'actions'  => array(
						array(
							'label' => __( 'Kuralları 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,
						'days_left'  => $days_left,
						'start_date' => $start_date,
					),
				);
			}
		}

		return $notifications;
	}
}
