<?php

require_once BASE_PATH . '/config/database.php';

class OwnerCustomerSpa
{
    private PDO $db;

    public function __construct()
    {
        $this->db =
            (new Database())->connect();
    }


    /*
    |--------------------------------------------------------------------------
    | THỐNG KÊ NHIỀU KHÁCH HÀNG
    |--------------------------------------------------------------------------
    */

    public function getStatsForCustomers(
        array $customerIds
    ): array {

        $customerIds =
            array_values(
                array_unique(
                    array_filter(
                        array_map(
                            'intval',
                            $customerIds
                        ),
                        fn ($id) => $id > 0
                    )
                )
            );


        if (!$customerIds) {
            return [];
        }


        $placeholders = [];
        $params = [];


        foreach (
            $customerIds
            as $index => $customerId
        ) {

            $placeholder =
                ':customer_' . $index;

            $placeholders[] =
                $placeholder;

            $params[
                'customer_' . $index
            ] = $customerId;
        }


        $sql = '
            SELECT
                customer_id,

                COUNT(*) AS booking_count,

                SUM(
                    CASE
                        WHEN status = "completed"
                        THEN 1
                        ELSE 0
                    END
                ) AS completed_booking_count,

                SUM(
                    CASE
                        WHEN status IN (
                            "pending",
                            "confirmed"
                        )
                        AND TIMESTAMP(
                            booking_date,
                            start_time
                        ) >= NOW()
                        THEN 1
                        ELSE 0
                    END
                ) AS upcoming_booking_count

            FROM bookings

            WHERE customer_id IN (
                ' . implode(
                    ', ',
                    $placeholders
                ) . '
            )

            GROUP BY customer_id
        ';


        $stmt =
            $this->db->prepare(
                $sql
            );


        $stmt->execute(
            $params
        );


        $result = [];


        foreach (
            $stmt->fetchAll()
            as $row
        ) {

            $customerId =
                (int) $row[
                    'customer_id'
                ];


            $result[$customerId] = [

                'booking_count' =>
                    (int) $row[
                        'booking_count'
                    ],

                'completed_booking_count' =>
                    (int) $row[
                        'completed_booking_count'
                    ],

                'upcoming_booking_count' =>
                    (int) $row[
                        'upcoming_booking_count'
                    ]
            ];
        }


        return $result;
    }


    /*
    |--------------------------------------------------------------------------
    | THỐNG KÊ 1 KHÁCH HÀNG
    |--------------------------------------------------------------------------
    */

    public function getStatsForCustomer(
        int $customerId
    ): array {

        $stmt =
            $this->db->prepare(
                '
                SELECT

                    COUNT(*) AS booking_count,

                    SUM(
                        CASE
                            WHEN status = "completed"
                            THEN 1
                            ELSE 0
                        END
                    ) AS completed_booking_count,

                    SUM(
                        CASE
                            WHEN status IN (
                                "pending",
                                "confirmed"
                            )
                            AND TIMESTAMP(
                                booking_date,
                                start_time
                            ) >= NOW()
                            THEN 1
                            ELSE 0
                        END
                    ) AS upcoming_booking_count

                FROM bookings

                WHERE customer_id = :customer_id
                '
            );


        $stmt->execute([
            'customer_id' =>
                $customerId
        ]);


        $row =
            $stmt->fetch()
            ?: [];


        return [

            'booking_count' =>
                (int) (
                    $row[
                        'booking_count'
                    ]
                    ?? 0
                ),

            'completed_booking_count' =>
                (int) (
                    $row[
                        'completed_booking_count'
                    ]
                    ?? 0
                ),

            'upcoming_booking_count' =>
                (int) (
                    $row[
                        'upcoming_booking_count'
                    ]
                    ?? 0
                )
        ];
    }


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

    public function getHistoryForCustomer(
    int $customerId,
    int $limit = 20
): array {

    $limit =
        max(
            1,
            min(
                100,
                $limit
            )
        );


    $stmt =
        $this->db->prepare(
            '
            SELECT
                b.id,
                b.booking_code,

                b.service_name_snapshot,
                b.service_price_snapshot,
                b.duration_minutes_snapshot,

                b.pet_name,
                b.pet_type,
                b.pet_breed,
                b.pet_weight,

                b.booking_date,
                b.start_time,
                b.end_time,

                b.status,
                b.customer_note,
                b.owner_note,

                b.created_at

            FROM bookings b

            WHERE b.customer_id = :customer_id

            ORDER BY
                b.booking_date DESC,
                b.start_time DESC,
                b.id DESC

            LIMIT ' . $limit
        );


    $stmt->execute([
        'customer_id' =>
            $customerId
    ]);


    return $stmt->fetchAll();
}
}