<?php

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

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

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

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


class OwnerCustomerController
    extends OwnerBaseController
{
    private User $userModel;

    private CustomerAddress $addressModel;

    private OwnerCustomerSpa $spaModel;


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


        $this->userModel =
            new User();


        $this->addressModel =
            new CustomerAddress();


        $this->spaModel =
            new OwnerCustomerSpa();
    }


    /*
    |--------------------------------------------------------------------------
    | DANH SÁCH KHÁCH HÀNG
    |--------------------------------------------------------------------------
    */

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


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


        $perPage = 15;


        $totalCustomers =
            $this->userModel
                ->countCustomersForOwner(
                    $search
                );


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


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


        $customers =
            $this->userModel
                ->getCustomersForOwner(
                    $search,
                    $perPage,
                    ($page - 1)
                    * $perPage
                );


        /*
        |--------------------------------------------------------------------------
        | LẤY THỐNG KÊ SPA
        |--------------------------------------------------------------------------
        */

        $customerIds =
            array_map(
                fn ($customer) =>
                    (int) $customer['id'],
                $customers
            );


        $spaStats =
            $this->spaModel
                ->getStatsForCustomers(
                    $customerIds
                );


        foreach (
            $customers
            as &$customer
        ) {

            $customerId =
                (int) $customer['id'];


            $stats =
                $spaStats[$customerId]
                ?? [
                    'booking_count' => 0,
                    'completed_booking_count' => 0,
                    'upcoming_booking_count' => 0
                ];


            $customer['booking_count'] =
                $stats[
                    'booking_count'
                ];


            $customer[
                'completed_booking_count'
            ] =
                $stats[
                    'completed_booking_count'
                ];


            $customer[
                'upcoming_booking_count'
            ] =
                $stats[
                    'upcoming_booking_count'
                ];
        }


        unset($customer);


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


    /*
    |--------------------------------------------------------------------------
    | CHI TIẾT KHÁCH HÀNG
    |--------------------------------------------------------------------------
    */

    public function detail(): void
    {
        $customerId =
            (int) (
                $_GET['id']
                ?? 0
            );


        $customer =
            $this->userModel
                ->findCustomerForOwner(
                    $customerId
                );


        if (!$customer) {

            http_response_code(
                404
            );


            require BASE_PATH
                . '/app/views/errors/404.php';


            return;
        }


        /*
        |--------------------------------------------------------------------------
        | THỐNG KÊ SPA
        |--------------------------------------------------------------------------
        */

        $spaStats =
            $this->spaModel
                ->getStatsForCustomer(
                    $customerId
                );


        /*
        |--------------------------------------------------------------------------
        | LỊCH SỬ SPA
        |--------------------------------------------------------------------------
        */

        $bookings =
            $this->spaModel
                ->getHistoryForCustomer(
                    $customerId,
                    20
                );


        /*
        |--------------------------------------------------------------------------
        | GIỮ NGUYÊN ĐỊA CHỈ HIỆN TẠI
        |--------------------------------------------------------------------------
        */

        $addresses =
            $this->addressModel
                ->getByUserId(
                    $customerId
                );


        require BASE_PATH
            . '/app/views/owner/customers/detail.php';
    }
}