<?php
declare(strict_types=1);

namespace WP_AUX\Services\AuxFramework;

use WP_AUX\Entities\AuxFramework\Partials\CardEntity;
use WP_AUX\Entities\AuxFramework\Partials\CardGroupEntity;
use WP_AUX\Entities\AuxFramework\Partials\CardGroupSetEntity;
use WP_AUX\Entities\AuxFramework\Partials\SectionEntity;
use WP_AUX\Entities\PostsQuery\PostsQueryEntity;
use WP_AUX\Services\PostService;

/**
 * Services for working with AUX Card Groups
 * @since 1.0.0
 */
class CardGroupService
{
  /**
   * Add a list of posts as an array or post query to a card group
   * @since 1.0.0
   */
  public static function addPostsToCardGroup(
    CardGroupEntity $cardGroup,
    array|PostsQueryEntity $posts,
    bool $includeImage = true,
    bool $includeCategories = false,
    string $cardSet = 'primary'
  ): ?CardGroupSetEntity {
    foreach ($posts as $post) {
      $postCard = PostService::postCard($post, $includeImage, $includeCategories);
      $cardGroup->addCardEntity($postCard, $cardSet);
    }

    return $cardGroup->getCardSet($cardSet);
  }

  /**
   * Get featured card group content by group type and cards layout type
   *
   * @param string $groupType 'inline' or 'standard' featured card group
   * @param string $cardsLayout 'inline' or 'standard' type for additional cards
   * @since 1.0.1
   */
  public static function createFeatureCardGroup(SectionEntity $section, string $groupType, string $cardsLayout): string
  {
    $cards = aux_get_field('card_for_featured');

    // Create the featured card, with type matching the $groupType
    $featuredCard = CardEntity::create(
      aux_get_field('featured_card_heading'),
      aux_get_field('featured_card_content')
    )
      ->setType($groupType)
      ->setSuperheader(aux_get_field('featured_card_superheader'))
      ->setSubheader(aux_get_field('featured_card_subheader'))
      ->setIcon(aux_get_field('featured_card_icon'))
      ->setImage(aux_get_field('featured_card_image'))
      ->setCtaType(aux_get_field('featured_card_cta_type'))
      ->setCta(aux_get_field('featured_card_cta_link'))
      ->setHasIntro($section->hasIntro()) // check if the section has an intro
      ->setIsFeature(true);

    // Create the additional cards, with type matching the $cardsLayout
    $cardsHtml = '';
    if ($cards) {
      foreach ($cards as $card) {
        $cardEntity = CardEntity::create($card['card_heading'], $card['card_content'])
          ->setType($cardsLayout)
          ->setSuperheader($card['card_superheader'])
          ->setSubheader($card['card_subheader'])
          ->setIcon($card['card_icon'])
          ->setImage($card['card_image'])
          ->setHasIntro($section->hasIntro())
          ->setCta($card['card_cta'])
          ->setCtaType(aux_get_field('non_featured_cards_cta_type'));

        if (!empty($card['card_ctas'])) {
          foreach ($card['card_ctas'] as $cardCta) {
            $cardEntity->addCta($cardCta['card_link']);
          }
        }

        $cardsHtml .= $cardEntity;
      }
    }

    /**
     * If the group type is inline, and the cards layout is inline, return a featured
     * cards container. Otherwise, return a card containter with a featured card,
     * and a standard cards list
     */
    if ('inline' === $groupType && 'inline' === $cardsLayout) {
      $content = <<<HTML
        <div class="card-container">
          $featuredCard
          $cardsHtml
        </div>
      HTML;
    } else {
      $cardsCount = !empty($cards) ? count($cards) : 0;

      $content = <<<HTML
        <div class="card-container">
          <div class="featured">
            $featuredCard
          </div>
          <div class="standard" data-cards="$cardsCount">
            $cardsHtml
          </div>
        </div>
      HTML;
    }

    return $content;
  }
}
