<?php
class BeautyBooking {
    private $conn;
    private $table_name = "beauty_bookings";

    public $id;
    public $user_id;
    public $name;
    public $phone;
    public $email;
    public $service_id;
    public $appointment_date;
    public $appointment_time;
    public $status;
    public $notes;
    public $last_error;

    public function __construct($db) {
        $this->conn = $db;
    }

    public function getAllBookings() {
        $query = "SELECT b.*, s.name as service_name, s.price as service_price
                  FROM " . $this->table_name . " b
                  LEFT JOIN beauty_services s ON b.service_id = s.id
                  ORDER BY b.created_at DESC";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function getBookingsByUser($user_id) {
        $query = "SELECT b.*, s.name as service_name, s.price as service_price
                  FROM " . $this->table_name . " b
                  LEFT JOIN beauty_services s ON b.service_id = s.id
                  WHERE b.user_id = ?
                  ORDER BY b.created_at DESC";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $user_id);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function create() {
        try {
            $query = "INSERT INTO " . $this->table_name . "
                    SET user_id=:user_id, name=:name, phone=:phone, email=:email,
                        service_id=:service_id, appointment_date=:appointment_date,
                        appointment_time=:appointment_time, status=:status, notes=:notes";

            $stmt = $this->conn->prepare($query);

            $this->user_id = htmlspecialchars(strip_tags($this->user_id ?? ''));
            $this->name = htmlspecialchars(strip_tags($this->name));
            $this->phone = htmlspecialchars(strip_tags($this->phone));
            $this->email = htmlspecialchars(strip_tags($this->email ?? ''));
            $this->service_id = htmlspecialchars(strip_tags($this->service_id));
            $this->appointment_date = htmlspecialchars(strip_tags($this->appointment_date));
            $this->appointment_time = htmlspecialchars(strip_tags($this->appointment_time));
            $this->status = $this->status ?? 'pending';
            $this->notes = htmlspecialchars(strip_tags($this->notes ?? ''));

            $stmt->bindParam(":user_id", $this->user_id);
            $stmt->bindParam(":name", $this->name);
            $stmt->bindParam(":phone", $this->phone);
            $stmt->bindParam(":email", $this->email);
            $stmt->bindParam(":service_id", $this->service_id);
            $stmt->bindParam(":appointment_date", $this->appointment_date);
            $stmt->bindParam(":appointment_time", $this->appointment_time);
            $stmt->bindParam(":status", $this->status);
            $stmt->bindParam(":notes", $this->notes);

            if($stmt->execute()) {
                $this->id = $this->conn->lastInsertId();
                return true;
            }

            return false;
        } catch (PDOException $e) {
            // Store the error message for debugging
            $this->last_error = $e->getMessage();
            error_log("Database error in BeautyBooking::create(): " . $e->getMessage());
            return false;
        }
    }

    public function updateStatus() {
        $query = "UPDATE " . $this->table_name . " SET status = ? WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->status);
        $stmt->bindParam(2, $this->id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function isTimeSlotAvailable($service_id, $date, $time) {
        $query = "SELECT id FROM " . $this->table_name . "
                  WHERE service_id = ? AND appointment_date = ? AND appointment_time = ?
                  AND status IN ('pending', 'confirmed')";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $service_id);
        $stmt->bindParam(2, $date);
        $stmt->bindParam(3, $time);
        $stmt->execute();

        return $stmt->rowCount() == 0;
    }
}
?>