<?php
/**
 * Online Solution Sync Featured Groups.
 *
 * @package northmill-online-shared
 */

declare(strict_types=1);

namespace Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Sync;

use Northmill_Online_Shared\Abstract_Shared_Base;
use Northmill_Online_Shared\Abstracts\Hub_API\Abstract_API_Base;
use Northmill_Online_Shared\Helpers\WP_Data_Sync_Helpers;
use Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Models\Abstract_Online_Solution_Settings_Model;
use Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Models\Properties\Settings_Featured_Group_Model;
use Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Models\Properties\Settings_Image_Model;

defined( 'ABSPATH' ) || exit;

/**
 * Class Online_Solutions_Sync_Images
 */
readonly class Online_Solution_Sync_Featured_Groups {

	/**
	 * Get WP option name for featured groups.
	 *
	 * @return string
	 */
	private static function get_option_name(): string {
		return 'nms_online_solution_featured_groups_' . Abstract_Shared_Base::base_instance()->package_info->get_service_name_underscored();
	}

	/**
	 * Get featured group IDs from DB.
	 *
	 * @return string[]
	 *
	 * @used Northmill Express Order
	 */
	public static function get_groups(): array {
		return explode( '|', get_option( self::get_option_name(), '' ) );
	}

	/**
	 * Load featured groups from DB.
	 *
	 * @param Abstract_Online_Solution_Settings_Model $settings_model The settings model to populate with featured groups.
	 *
	 * @used Northmill Express Order
	 * @used Northmill Webshop Theme
	 */
	public static function maybe_load_featured_groups( Abstract_Online_Solution_Settings_Model $settings_model ): void {
		$featured_groups = explode( '|', get_option( self::get_option_name(), '' ) );

		if ( ! empty( $featured_groups ) ) {
			foreach ( $featured_groups as $fg_id ) {
				$term = get_term( WP_Data_Sync_Helpers::get_term( $fg_id, 'product_cat', 'mf_group_id' ), 'product_cat' );

				if ( is_a( $term, 'WP_Term' ) ) {
					$images       = array();
					$thumbnail_id = get_term_meta( $term->term_id, 'thumbnail_id', true );
					if ( $thumbnail_id ) {
						$attachment = wp_get_attachment_metadata( $thumbnail_id );
						if ( $attachment ) {
							$images[] = new Settings_Image_Model(
								array(
									'Name'       => basename( $attachment['file'] ),
									'IsMainFile' => true,
									'URL'        => wp_get_attachment_image_url( $thumbnail_id ),
								)
							);
						}
					}

					$settings_model->FeaturedGroups[] = new Settings_Featured_Group_Model(
						array(
							'GroupId'     => $fg_id,
							'WPId'        => $term->term_id,
							'GroupName'   => $term->name,
							'GroupImages' => $images,
						)
					);
				}
			}
		}
	}

	/**
	 * Store featured groups in DB from model.
	 *
	 * @param Abstract_Online_Solution_Settings_Model $settings_model The settings model containing featured groups to store.
	 * @param Abstract_API_Base                       $api API instance to use for image import.
	 *
	 * @return string[] List of GroupId property from Hub.
	 *
	 * @used Northmill Express Order
	 * @used Northmill Webshop Theme
	 */
	public static function maybe_store_featured_groups( Abstract_Online_Solution_Settings_Model $settings_model, Abstract_API_Base $api ): array {
		$featured_groups_ids = array();

		if ( empty( $settings_model->FeaturedGroups ) ) {
			update_option( self::get_option_name(), '' );
		} else {
			$featured_groups_ids = array_map(
				function ( $fd ) {
					return $fd->get_group_id();
				},
				$settings_model->FeaturedGroups
			);
			update_option( self::get_option_name(), implode( '|', $featured_groups_ids ) );

			foreach ( $settings_model->FeaturedGroups as $featured_group ) {
				$term_id      = WP_Data_Sync_Helpers::get_term( $featured_group->GroupId, 'product_cat', 'mf_group_id' );
				$group_images = $featured_group->get_group_images();

				if ( ! empty( $group_images ) ) {
					foreach ( $featured_group->get_group_images() as $image ) {
						WP_Data_Sync_Helpers::import_image( $image->get_id(), $image->get_name(), $image->get_url(), $api );
					}
				}

				if ( $term_id && ! empty( $group_images ) ) {
					$fg_main_image_id = Online_Solution_Sync_Images::get_main_attachment( $group_images );
					if ( $fg_main_image_id ) {
						update_term_meta( $term_id, 'thumbnail_id', $fg_main_image_id );
					} else {
						delete_term_meta( $term_id, 'thumbnail_id' );
					}
				} elseif ( $term_id ) {
					delete_term_meta( $term_id, 'thumbnail_id' );
				}
			}
		}

		return $featured_groups_ids;
	}
}
