<?php
/**
 * Class Noesia Cs Warranty Service.
 *
 * @package Noesia_Commerce_Suite
 */

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

/**
 * Class implementation.
 */
class Noesia_CS_Warranty_Service {
	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $order Order.
	 * @param mixed $item Item.
	 */
	public function get_item_warranty_status( $order, $item ) {
		$months = $this->get_warranty_months_for_item( $item );
		if ( ! $order || ! is_a( $order, 'WC_Order' ) || ! $months ) {
			return $this->neutral_status();
		}

		$base_date = $this->get_base_date_for_order( $order );
		if ( ! $base_date ) {
			return array(
				'label'   => Noesia_CS_Settings::get( 'warranty_unknown_text', __( 'Warranty not configured', 'noesia-commerce-suite' ) ),
				'state'   => 'neutral',
				'expiry'  => '',
				'months'  => $months,
			);
		}

		$expiry = clone $base_date;
		$expiry->modify( '+' . absint( $months ) . ' months' );
		$now     = new WC_DateTime();
		$active  = $expiry->getTimestamp() >= $now->getTimestamp();

		return array(
			'label'   => $active ? Noesia_CS_Settings::get( 'warranty_active_text', __( 'Warranty Active', 'noesia-commerce-suite' ) ) : Noesia_CS_Settings::get( 'warranty_expired_text', __( 'Warranty Expired', 'noesia-commerce-suite' ) ),
			'state'   => $active ? 'active' : 'expired',
			'expiry'  => $expiry->date_i18n( get_option( 'date_format' ) ),
			'months'  => $months,
		);
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $order Order.
	 */
	public function get_order_warranty_summary( $order ) {
		if ( ! $order || ! is_a( $order, 'WC_Order' ) ) {
			return $this->neutral_status();
		}

		$items = $order->get_items();
		if ( empty( $items ) ) {
			return $this->neutral_status();
		}

		$active_count   = 0;
		$expired_count  = 0;
		$latest_expiry  = '';
		$latest_ts      = 0;

		foreach ( $items as $item ) {
			$status = $this->get_item_warranty_status( $order, $item );
			if ( 'active' === $status['state'] ) {
				++$active_count;
			} elseif ( 'expired' === $status['state'] ) {
				++$expired_count;
			}

			if ( ! empty( $status['expiry'] ) ) {
				$timestamp = strtotime( $status['expiry'] );
				if ( $timestamp && $timestamp > $latest_ts ) {
					$latest_ts      = $timestamp;
					$latest_expiry  = $status['expiry'];
				}
			}
		}

		if ( $active_count > 0 ) {
			return array(
				'label'   => Noesia_CS_Settings::get( 'warranty_active_text', __( 'Warranty Active', 'noesia-commerce-suite' ) ),
				'state'   => 'active',
				'expiry'  => $latest_expiry,
				'count'   => $active_count,
			);
		}

		if ( $expired_count > 0 ) {
			return array(
				'label'   => Noesia_CS_Settings::get( 'warranty_expired_text', __( 'Warranty Expired', 'noesia-commerce-suite' ) ),
				'state'   => 'expired',
				'expiry'  => $latest_expiry,
				'count'   => $expired_count,
			);
		}

		return $this->neutral_status();
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $order Order.
	 */
	public function order_has_active_warranty( $order ) {
		$summary = $this->get_order_warranty_summary( $order );
		return 'active' === $summary['state'];
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $order Order.
	 */
	public function get_base_date_for_order( $order ) {
		if ( ! $order || ! is_a( $order, 'WC_Order' ) ) {
			return null;
		}

		$mode = Noesia_CS_Settings::get( 'warranty_calculation_mode', 'order_date' );
		if ( 'completed_date' === $mode && $order->get_date_completed() ) {
			return $order->get_date_completed();
		}

		return $order->get_date_created();
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $item Item.
	 */
	private function get_warranty_months_for_item( $item ) {
		$months      = absint( Noesia_CS_Settings::get( 'default_warranty_months', 12 ) );
		$product_id  = 0;
		$product     = null;
		if ( $item && method_exists( $item, 'get_product_id' ) ) {
			$product_id = absint( $item->get_product_id() );
		}
		if ( $item && method_exists( $item, 'get_product' ) ) {
			$product = $item->get_product();
		}

		/**
		 * Allows future premium/add-on logic to override warranty duration without mixing Pro code into the free package.
		 *
		 * @param int   $months     Default warranty months.
		 * @param mixed $item       WooCommerce order item.
		 * @param int   $product_id Product ID.
		 * @param mixed $product    WooCommerce product object when available.
		 */
		return absint( apply_filters( 'noesia_cs_item_warranty_months', $months, $item, $product_id, $product ) );
	}

	/**
	 * Handles the related plugin operation.
	 */
	private function neutral_status() {
		return array(
			'label'   => Noesia_CS_Settings::get( 'warranty_unknown_text', __( 'Warranty not configured', 'noesia-commerce-suite' ) ),
			'state'   => 'neutral',
			'expiry'  => '',
			'count'   => 0,
		);
	}
}
