<?php

require_once
    __DIR__ .
    '/../data/testimonials.php';


/* =========================================================
   HOMEPAGE SHOWS MAXIMUM 6
========================================================= */

$homepageTestimonials =
    array_slice(
        $testimonials,
        0,
        3
    );


$totalTestimonials =
    count($testimonials);

?>


<section class="cbx-testimonials" id="testimonials">

    <div class="cbx-container">


        <!-- =====================================
             HEADING
        ====================================== -->

        <div class="cbx-testimonials-heading">


            <div class="cbx-testimonials-tag">

                <i class="fa-solid fa-comments"></i>

                <span>
                    WHAT PEOPLE ARE SAYING
                </span>

            </div>


            <h2>

                Experiences speak

                <span>
                    louder than promises.
                </span>

            </h2>


            <p>
                Hear directly from people who have experienced
                Complete Bliss and shared their thoughts with us.
            </p>


        </div>



        <?php if (!empty($testimonials)): ?>


            <!-- =====================================
                 TESTIMONIAL CARDS
            ====================================== -->

            <div class="cbx-testimonial-grid">


                <?php foreach ($homepageTestimonials as $testimonial): ?>


                    <?php

                    $name =
                        trim(
                            $testimonial['name']
                            ?? 'Complete Bliss Member'
                        );

                    $location =
                        trim(
                            $testimonial['location']
                            ?? ''
                        );

                    $message =
                        trim(
                            $testimonial['message']
                            ?? ''
                        );

                    $rating =
                        max(
                            1,
                            min(
                                5,
                                (int)(
                                    $testimonial['rating']
                                    ?? 5
                                )
                            )
                        );

                    $initial =
                        strtoupper(
                            mb_substr(
                                $name,
                                0,
                                1
                            )
                        );

                    ?>


                    <article class="cbx-testimonial-card">


                        <!-- QUOTE -->

                        <div class="cbx-testimonial-quote">

                            <i class="fa-solid fa-quote-left"></i>

                        </div>



                        <!-- STARS -->

                        <div
                            class="cbx-testimonial-stars"
                            aria-label="<?= $rating ?> out of 5 stars"
                        >


                            <?php for ($star = 1; $star <= 5; $star++): ?>

                                <i
                                    class="<?= $star <= $rating
                                        ? 'fa-solid'
                                        : 'fa-regular' ?>
                                        fa-star"
                                ></i>

                            <?php endfor; ?>


                        </div>



                        <!-- MESSAGE -->

                        <blockquote>

                            “<?= htmlspecialchars($message) ?>”

                        </blockquote>



                        <!-- MEMBER -->

                        <div class="cbx-testimonial-member">


                            <div class="cbx-testimonial-avatar">

                                <?= htmlspecialchars($initial) ?>

                            </div>


                            <div>

                                <strong>
                                    <?= htmlspecialchars($name) ?>
                                </strong>


                                <?php if ($location !== ''): ?>

                                    <span>

                                        <i class="fa-solid fa-location-dot"></i>

                                        <?= htmlspecialchars($location) ?>

                                    </span>

                                <?php endif; ?>

                            </div>


                            <div class="cbx-testimonial-verified">

                                <i class="fa-solid fa-circle-check"></i>

                                <small>
                                    Feedback
                                </small>

                            </div>


                        </div>


                    </article>


                <?php endforeach; ?>


            </div>

<?php if ($totalTestimonials > 3): ?>

    <div class="cbx-testimonial-view-all">

        <div>

            <span>
                <?= number_format($totalTestimonials) ?>
            </span>

            <p>
                genuine member experiences and counting
            </p>

        </div>


        <a href="testimonials.php">

            View All Testimonials

            <i class="fa-solid fa-arrow-right"></i>

        </a>

    </div>

<?php endif; ?>

            <!-- =====================================
                 BOTTOM MESSAGE
            ====================================== -->

            <div class="cbx-testimonial-bottom">


                <div class="cbx-testimonial-bottom-icon">

                    <i class="fa-solid fa-heart"></i>

                </div>


                <div>

                    <span>
                        YOUR EXPERIENCE MATTERS
                    </span>

                    <h3>
                        We value genuine feedback because it helps us
                        serve members better.
                    </h3>

                </div>


                <a href="register.php">

                    Join Complete Bliss

                    <i class="fa-solid fa-arrow-right"></i>

                </a>


            </div>



        <?php else: ?>


            <!-- =====================================
                 EMPTY STATE
            ====================================== -->

            <div class="cbx-testimonial-empty">


                <div class="cbx-testimonial-empty-icon">

                    <i class="fa-solid fa-comments"></i>

                </div>


                <h3>
                    Member Stories Are Coming
                </h3>


                <p>
                    Genuine feedback from Complete Bliss members
                    will be shared here.
                </p>


            </div>


        <?php endif; ?>


    </div>

</section>