<?php

require_once BASE_PATH . '/app/controllers/owner/OwnerBaseController.php';
require_once BASE_PATH . '/app/models/Booking.php';
require_once BASE_PATH . '/app/models/BookingStatusLog.php';
require_once BASE_PATH . '/app/models/SpaActivityLog.php';

class OwnerBookingController extends OwnerBaseController
{
    private Booking $bookings;
    private SpaActivityLog $activityLog;
    public function __construct() { $this->requireOwner(); $this->bookings = new Booking(); $this->activityLog = new SpaActivityLog(); }
    public function index(): void
    {
        $filters = ['status' => (string) ($_GET['status'] ?? ''), 'date' => (string) ($_GET['date'] ?? ''), 'q' => (string) ($_GET['q'] ?? '')];
        $page = max(1, (int) ($_GET['page'] ?? 1));
        $perPage = 20;
        try {
            $totalBookings = $this->bookings->countForOwner($filters);
        } catch (InvalidArgumentException) {
            $filters['date'] = '';
            $this->flashError('Ngày lọc không hợp lệ.');
            $totalBookings = $this->bookings->countForOwner($filters);
        }
        $totalPages = max(1, (int) ceil($totalBookings / $perPage));
        $page = min($page, $totalPages);
        $bookings = $this->bookings->getForOwner($filters, $perPage, ($page - 1) * $perPage);
        require BASE_PATH . '/app/views/owner/bookings/index.php';
    }
    public function detail(): void { $booking = $this->bookings->findForOwner((int) ($_GET['id'] ?? 0)); if (!$booking) { http_response_code(404); require BASE_PATH . '/app/views/errors/404.php'; return; } $statusLogs = (new BookingStatusLog())->getForBooking((int) $booking['id']); require BASE_PATH . '/app/views/owner/bookings/detail.php'; }
    public function updateStatus(): void
    {
        $this->requirePost(); $this->validateCsrf(); $id = (int) ($_POST['booking_id'] ?? 0);
        try { $before = $this->bookings->findForOwner($id); $booking = $this->bookings->updateStatus($id, (int) $_SESSION['user']['id'], (string) ($_POST['status'] ?? ''), trim((string) ($_POST['owner_note'] ?? '')) ?: null); $actions = ['confirmed' => 'owner.booking.confirm', 'in_progress' => 'owner.booking.start', 'completed' => 'owner.booking.complete', 'cancelled' => 'owner.booking.cancel']; $this->activityLog->record((int) $_SESSION['user']['id'], $actions[$booking['status']], 'booking', $id, ['from_status' => $before['status'] ?? null, 'to_status' => $booking['status']]); $this->flashSuccess('Đã cập nhật trạng thái lịch.'); }
        catch (Throwable $e) { $this->flashError($e->getMessage()); }
        $this->detailRedirect($id);
    }
    public function updateNote(): void
    {
        $this->requirePost(); $this->validateCsrf(); $id = (int) ($_POST['booking_id'] ?? 0);
        try { $this->bookings->updateOwnerNote($id, trim((string) ($_POST['owner_note'] ?? ''))); $this->activityLog->record((int) $_SESSION['user']['id'], 'owner.booking.note_update', 'booking', $id); $this->flashSuccess('Đã lưu ghi chú.'); }
        catch (Throwable $e) { $this->flashError($e->getMessage()); }
        $this->detailRedirect($id);
    }
    private function detailRedirect(int $id): never { header('Location: ' . BASE_URL . '/?url=owner/booking&id=' . $id); exit; }
}
