<?php

$post_faqs = get_posts(array(
  'post_type' => 'faq',
  'posts_per_page' => -1,
  'include' => $attributes['postIds']
));


$taxonomies = [];
if (!empty($attributes['taxonomiesIds'])) {
  // Get taxonomies in specified order using orderby parameter
  $taxonomies = get_terms(array(
    'taxonomy' => 'faq_category',
    'hide_empty' => false,
    'include' => $attributes['taxonomiesIds'],
    'orderby' => 'include' // This will maintain order from taxonomiesIds
  ));
}

//generate uuid for all
$all_id = uniqid('faq-all-');

//add uuid to $taxonomies
foreach ($taxonomies as $taxonomy) {
  $taxonomy->uuid = uniqid('faq-category-');
}

// Generate JSON-LD if enabled
$jsonld = [];
if ($attributes['createJsonLd']) {
  $jsonld = [
    '@context' => 'https://schema.org',
    '@type' => 'FAQPage',
    'mainEntity' => []
  ];

  foreach ($post_faqs as $post_faq) {
    $content = wp_strip_all_tags($post_faq->post_content);
    $jsonld['mainEntity'][] = [
      '@type' => 'Question',
      'name' => $post_faq->post_title,
      'acceptedAnswer' => [
        '@type' => 'Answer',
        'text' => $content
      ]
    ];
  }
}

$isCustomColor = $attributes['isCustomColor'] ?? false;

$style = "
  --orphic-wp-block-faq-custom-background-color: {$attributes['backgroundColor']};
  --orphic-wp-block-faq-custom-text-color: {$attributes['textColor']};
  --orphic-wp-block-faq-custom-border-color: {$attributes['borderColor']};
  --orphic-wp-block-faq-custom-primary-color: {$attributes['primaryColor']};
";


?>
<div <?php echo get_block_wrapper_attributes(); ?>>
  <div class="orphic-wp-block-faq__container <?php if ($isCustomColor): ?>customColor<?php endif; ?>" <?php if ($isCustomColor): ?> style="<?php echo $style; ?>" <?php endif; ?>>

    <?php if ($attributes['showFilterBar']) : ?>
      <div class="orphic-wp-block-faq__filter">
        <div class="orphic-wp-block-faq__filter-items">
          <span class="orphic-wp-block-faq__filter-item active" data-id="<?php echo $all_id; ?>">All</span>
          <?php foreach ($taxonomies as $taxonomy) : ?>
            <span class="orphic-wp-block-faq__filter-item" data-id="<?php echo $taxonomy->uuid; ?>"><?php echo $taxonomy->name; ?></span>
          <?php endforeach; ?>
        </div>
        <span class="orphic-wp-block-faq__filter-follower"></span>
      </div>
    <?php endif; ?>

    <div class="orphic-wp-block-faq__questions-panels">
      <div class="orphic-wp-block-faq__questions-panel all active" data-id="<?php echo $all_id; ?>">
        <?php
        foreach ($post_faqs as $post_faq) :
        ?>
          <div class="orphic-wp-block-faq__questions-faq-item">
            <!-- title -->
            <div class="orphic-wp-block-faq__questions-faq-item-header">
              <span class="orphic-wp-block-faq__questions-faq-item-title"><?php echo $post_faq->post_title; ?></span>
              <span class="orphic-block-and-functions-FAQ__section__icon">
                <span class="orphic-block-and-functions-FAQ__section__icon__left"></span>
                <span class="orphic-block-and-functions-FAQ__section__icon__right"></span>
              </span>
            </div>
            <!-- content -->
            <div class="orphic-wp-block-faq__questions-faq-item-content-wrapper">
              <div class="orphic-wp-block-faq__questions-faq-item-content">
                <?php echo $post_faq->post_content; ?>
              </div>
              <div class="orphic-wp-block-faq__questions-faq-item-content-line">
              </div>
            </div>
          </div>
        <?php
        endforeach;
        ?>
      </div>
      <?php if ($attributes['showFilterBar']) : ?>
        <?php foreach ($taxonomies as $taxonomy) : ?>
          <div class="orphic-wp-block-faq__questions-panel" data-id="<?php echo $taxonomy->uuid; ?>">
            <?php
            foreach ($post_faqs as $post_faq) :
              $post_terms = wp_get_post_terms($post_faq->ID, 'faq_category', array('fields' => 'ids'));
              if (in_array($taxonomy->term_id, $post_terms)) :
            ?>
                <div class="orphic-wp-block-faq__questions-faq-item">
                  <!-- title -->
                  <div class="orphic-wp-block-faq__questions-faq-item-header">
                    <span class="orphic-wp-block-faq__questions-faq-item-title"><?php echo $post_faq->post_title; ?></span>
                    <span class="orphic-block-and-functions-FAQ__section__icon">
                      <span class="orphic-block-and-functions-FAQ__section__icon__left"></span>
                      <span class="orphic-block-and-functions-FAQ__section__icon__right"></span>
                    </span>
                  </div>
                  <!-- content -->
                  <div class="orphic-wp-block-faq__questions-faq-item-content-wrapper">
                    <div class="orphic-wp-block-faq__questions-faq-item-content">
                      <?php echo $post_faq->post_content; ?>
                    </div>
                    <div class="orphic-wp-block-faq__questions-faq-item-content-line">
                    </div>
                  </div>
                </div>
            <?php
              endif;
            endforeach;
            ?>
          </div>
        <?php endforeach; ?>
      <?php endif; ?>
    </div>
  </div>
</div>
<?php if ($attributes['createJsonLd']) : ?>
  <!-- Schema optimized by Orphic -->
  <script type="application/ld+json">
    <?php echo  wp_json_encode($jsonld) ?>;
  </script>
<?php endif; ?>