<?php
require_once __DIR__ . '/../models/beautyservice.php';
require_once __DIR__ . '/../models/beautybooking.php';

class BeautyController {
    private $db;
    private $service;
    private $booking;

    public function __construct() {
        $database = new Database();
        $this->db = $database->getConnection();
        $this->service = new BeautyService($this->db);
        $this->booking = new BeautyBooking($this->db);
    }

    public function getServices() {
        $category = isset($_GET['category']) ? $_GET['category'] : null;
        $search = isset($_GET['search']) ? $_GET['search'] : null;

        if ($search) {
            $services = $this->service->searchServices($search);
        } elseif ($category) {
            $services = $this->service->getServicesByCategory($category);
        } else {
            $services = $this->service->getAllServices();
        }

        http_response_code(200);
        echo json_encode(array("services" => $services));
    }

    public function getService($params) {
        $service_id = $params['id'];
        $service = $this->service->getServiceById($service_id);

        if ($service) {
            http_response_code(200);
            echo json_encode($service);
        } else {
            http_response_code(404);
            echo json_encode(array("message" => "Service not found"));
        }
    }

    public function createBooking() {
        $data = json_decode(file_get_contents("php://input"));

        if (!isset($data->name) || !isset($data->phone) || !isset($data->service_id) ||
            !isset($data->appointment_date) || !isset($data->appointment_time)) {
            http_response_code(400);
            echo json_encode(array("message" => "Missing required fields"));
            return;
        }

        // Check if time slot is available
        if (!$this->booking->isTimeSlotAvailable($data->service_id, $data->appointment_date, $data->appointment_time)) {
            http_response_code(400);
            echo json_encode(array("message" => "This time slot is not available"));
            return;
        }

        $this->booking->user_id = $data->user_id ?? null;
        $this->booking->name = $data->name;
        $this->booking->phone = $data->phone;
        $this->booking->email = $data->email ?? '';
        $this->booking->service_id = $data->service_id;
        $this->booking->appointment_date = $data->appointment_date;
        $this->booking->appointment_time = $data->appointment_time;
        $this->booking->notes = $data->notes ?? '';

        if ($this->booking->create()) {
            http_response_code(201);
            echo json_encode(array("message" => "Booking created successfully"));
        } else {
            http_response_code(503);
            echo json_encode(array("message" => "Unable to create booking"));
        }
    }

    public function getBookings() {
        $bookings = $this->booking->getAllBookings();
        http_response_code(200);
        echo json_encode(array("bookings" => $bookings));
    }

    public function getUserBookings($params) {
        $user_id = $params['user_id'];
        $bookings = $this->booking->getBookingsByUser($user_id);
        http_response_code(200);
        echo json_encode(array("bookings" => $bookings));
    }

    public function updateBookingStatus($params) {
        $data = json_decode(file_get_contents("php://input"));
        $booking_id = $params['id'];

        if (!isset($data->status)) {
            http_response_code(400);
            echo json_encode(array("message" => "Status is required"));
            return;
        }

        $this->booking->id = $booking_id;
        $this->booking->status = $data->status;

        if ($this->booking->updateStatus()) {
            http_response_code(200);
            echo json_encode(array("message" => "Booking status updated successfully"));
        } else {
            http_response_code(503);
            echo json_encode(array("message" => "Unable to update booking status"));
        }
    }
}
?>