<?php

require_once BASE_PATH
    . '/app/controllers/owner/OwnerBaseController.php';

require_once BASE_PATH
    . '/app/models/ProductReview.php';

require_once BASE_PATH
    . '/app/models/Notification.php';


class OwnerReviewController
    extends OwnerBaseController
{
    private ProductReview $reviewModel;
    private Notification $notificationModel;


    public function __construct()
    {
        $this->requireOwner();

        $this->reviewModel =
            new ProductReview();

        $this->notificationModel =
            new Notification();
    }


    public function index(): void
    {
        $search =
            trim(
                $_GET['search']
                ?? ''
            );


        $status =
            trim(
                $_GET['status']
                ?? ''
            );


        if (
            !in_array(
                $status,
                [
                    '',
                    'visible',
                    'hidden'
                ],
                true
            )
        ) {

            $status = '';
        }


        $page =
            max(
                1,
                (int)
                ($_GET['page']
                    ?? 1)
            );


        $perPage = 15;


        $totalReviews =
            $this->reviewModel
                ->countForOwner(
                    $search,
                    $status
                );


        $totalPages =
            max(
                1,
                (int)
                ceil(
                    $totalReviews
                    / $perPage
                )
            );


        $page =
            min(
                $page,
                $totalPages
            );


        $reviews =
            $this->reviewModel
                ->getForOwner(
                    $search,
                    $status,
                    $perPage,
                    ($page - 1)
                    * $perPage
                );


        require BASE_PATH
            . '/app/views/owner/reviews/index.php';
    }


    public function reply(): void
    {
        $this->requirePost();

        $this->validateCsrf();


        $id =
            (int)
            ($_POST['review_id']
                ?? 0);


        $reply =
            trim(
                $_POST['owner_reply']
                ?? ''
            );


        if ($reply === '') {

            $this->flashError(
                'Vui lòng nhập nội dung phản hồi.'
            );

            $this->redirect(
                'owner/reviews'
            );
        }


        $review =
            $this->reviewModel
                ->findById($id);


        if (!$review) {

            $this->flashError(
                'Đánh giá không tồn tại.'
            );

            $this->redirect(
                'owner/reviews'
            );
        }


        $this->reviewModel
            ->reply(
                $id,
                $reply
            );


        if (empty($review['owner_reply'])) {
            $this->notificationModel->create(
                (int) $review['customer_id'],
                'review.reply',
                'Cửa hàng đã phản hồi đánh giá',
                'Cửa hàng đã phản hồi đánh giá của bạn về '
                    . $review['product_name'] . '.',
                '?url=customer/reviews'
            );
        }


        $this->flashSuccess(
            'Đã phản hồi đánh giá.'
        );


        $this->redirect(
            'owner/reviews'
        );
    }


    public function toggle(): void
    {
        $this->requirePost();

        $this->validateCsrf();


        $id =
            (int)
            ($_POST['review_id']
                ?? 0);


        $this->reviewModel
            ->toggleStatus($id);


        $this->flashSuccess(
            'Đã cập nhật trạng thái đánh giá.'
        );


        $this->redirect(
            'owner/reviews'
        );
    }
}
