<?php

$totalReviews =
    (int)
    ($reviewSummary['total_reviews']
        ?? 0);


$averageRating =
    (float)
    ($reviewSummary['average_rating']
        ?? 0);


$roundedRating =
    (int)
    round($averageRating);

?>


<section
    class="product-review-section"
    id="product-reviews"
>

    <div class="container">

        <div class="row g-4">


            <!-- ==============================================
                 REVIEW SUMMARY
            =============================================== -->

            <div class="col-lg-4">

                <div class="product-review-summary">

                    <span class="review-summary-label">
                        ĐÁNH GIÁ SẢN PHẨM
                    </span>


                    <div class="review-average">

                        <?= number_format(
                            $averageRating,
                            1
                        ) ?>

                    </div>


                    <div
                        class="review-stars review-stars-large"
                    >

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

                            <?php if (
                                $i <= $roundedRating
                            ): ?>

                                <i
                                    class="bi bi-star-fill"
                                ></i>

                            <?php else: ?>

                                <i
                                    class="bi bi-star"
                                ></i>

                            <?php endif; ?>

                        <?php endfor; ?>

                    </div>


                    <p>

                        Dựa trên

                        <strong>
                            <?= $totalReviews ?>
                        </strong>

                        đánh giá

                    </p>


                    <?php if (
                        isset($_SESSION['user'])
                        && (
                            $_SESSION['user']['role']
                            ?? null
                        ) === 'customer'
                    ): ?>

                        <a
                            href="<?= BASE_URL ?>/?url=customer/reviews"
                            class="btn btn-outline-primary"
                        >

                            <i
                                class="bi bi-pencil-square me-1"
                            ></i>

                            Đánh giá sản phẩm đã mua

                        </a>

                    <?php else: ?>

                        <a
                            href="<?= BASE_URL ?>/?url=login"
                            class="btn btn-outline-primary"
                        >

                            Đăng nhập để đánh giá

                        </a>

                    <?php endif; ?>

                </div>

            </div>


            <!-- ==============================================
                 REVIEW LIST
            =============================================== -->

            <div class="col-lg-8">

                <div class="product-review-list">


                    <?php if (
                        empty($reviews)
                    ): ?>

                        <div
                            class="product-review-empty"
                        >

                            <div class="display-6">
                                ⭐
                            </div>

                            <h3>
                                Chưa có đánh giá
                            </h3>

                            <p>

                                Hãy là khách hàng đầu tiên
                                chia sẻ trải nghiệm về sản phẩm này.

                            </p>

                        </div>


                    <?php else: ?>


                        <?php foreach (
                            $reviews
                            as $review
                        ): ?>

                            <article
                                class="product-review-card"
                            >

                                <div
                                    class="product-review-card-header"
                                >

                                    <div
                                        class="review-customer"
                                    >

                                        <div
                                            class="review-avatar"
                                        >

                                            <?= htmlspecialchars(
                                                mb_strtoupper(
                                                    mb_substr(
                                                        $review[
                                                            'customer_name'
                                                        ],
                                                        0,
                                                        1
                                                    )
                                                )
                                            ) ?>

                                        </div>


                                        <div>

                                            <strong>

                                                <?= htmlspecialchars(
                                                    $review[
                                                        'customer_name'
                                                    ]
                                                ) ?>

                                            </strong>


                                            <small>

                                                <?= date(
                                                    'd/m/Y',
                                                    strtotime(
                                                        $review[
                                                            'created_at'
                                                        ]
                                                    )
                                                ) ?>

                                            </small>

                                        </div>

                                    </div>


                                    <div
                                        class="review-stars"
                                    >

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

                                            <?php if (
                                                $i <=
                                                (int)
                                                $review['rating']
                                            ): ?>

                                                <i
                                                    class="bi bi-star-fill"
                                                ></i>

                                            <?php else: ?>

                                                <i
                                                    class="bi bi-star"
                                                ></i>

                                            <?php endif; ?>

                                        <?php endfor; ?>

                                    </div>

                                </div>


                                <?php if (
                                    !empty(
                                        $review['comment']
                                    )
                                ): ?>

                                    <div
                                        class="review-comment"
                                    >

                                        <?= nl2br(
                                            htmlspecialchars(
                                                $review[
                                                    'comment'
                                                ]
                                            )
                                        ) ?>

                                    </div>

                                <?php endif; ?>


                                <!-- OWNER REPLY -->

                                <?php if (
                                    !empty(
                                        $review[
                                            'owner_reply'
                                        ]
                                    )
                                ): ?>

                                    <div
                                        class="review-owner-reply"
                                    >

                                        <div
                                            class="review-owner-reply-title"
                                        >

                                            <span>
                                                🐾
                                            </span>

                                            <strong>
                                                PET SPA phản hồi
                                            </strong>

                                        </div>


                                        <p>

                                            <?= nl2br(
                                                htmlspecialchars(
                                                    $review[
                                                        'owner_reply'
                                                    ]
                                                )
                                            ) ?>

                                        </p>

                                    </div>

                                <?php endif; ?>

                            </article>

                        <?php endforeach; ?>

                    <?php endif; ?>

                </div>

            </div>

        </div>

    </div>

</section>