/**
 * Database MCP Server - Bookings
 *
 * Provides access to demo booking data from Prisma/Supabase.
 * This is a high-value tool with 50,000+ token savings potential.
 *
 * Token Savings:
 * - Before: Load all 10,000 bookings into context (~50,000 tokens)
 * - After: Filter in code, return only summary (~200 tokens)
 * - Savings: 99.6% reduction
 */

import { callMCPTool } from "../../client.js";

export interface GetBookingsInput {
  status?: "pending" | "confirmed" | "cancelled";
  limit?: number;
  offset?: number;
  startDate?: string;
  endDate?: string;
}

export interface Booking {
  id: string;
  customerName: string;
  customerEmail: string;
  customerPhone: string;
  service: string;
  date: Date;
  time: string;
  status: string;
  notes?: string;
  createdAt: Date;
}

export interface BookingStats {
  total: number;
  pending: number;
  confirmed: number;
  cancelled: number;
}

/**
 * Get bookings from the database with optional filtering.
 * Data is filtered at the database level for efficiency.
 *
 * @param input - Query parameters for filtering bookings
 * @returns Array of booking objects
 *
 * @example
 * ```typescript
 * // Get all pending bookings
 * const pending = await getBookings({ status: 'pending' });
 *
 * // Get recent bookings with limit
 * const recent = await getBookings({ limit: 10 });
 *
 * // Get bookings in date range
 * const lastWeek = await getBookings({
 *   startDate: '2025-11-06',
 *   endDate: '2025-11-13'
 * });
 *
 * // Process in code to avoid token consumption
 * const all = await getBookings({ limit: 1000 });
 * const byService = all.reduce((acc, b) => {
 *   acc[b.service] = (acc[b.service] || 0) + 1;
 *   return acc;
 * }, {});
 * console.log('Top services:', Object.entries(byService).slice(0, 5));
 * ```
 */
export async function getBookings(
  input: GetBookingsInput = {},
): Promise<Booking[]> {
  return callMCPTool<Booking[]>("database__get_bookings", input);
}

/**
 * Get a single booking by ID
 *
 * @param bookingId - The booking ID
 * @returns Booking object or null if not found
 *
 * @example
 * ```typescript
 * const booking = await getBookingById('clx123abc');
 * if (booking) {
 *   console.log(`Booking for ${booking.customerName} on ${booking.date}`);
 * }
 * ```
 */
export async function getBookingById(
  bookingId: string,
): Promise<Booking | null> {
  return callMCPTool<Booking | null>("database__get_booking_by_id", {
    bookingId,
  });
}

/**
 * Get booking statistics (counts by status).
 * This is more efficient than fetching all bookings and counting in code.
 *
 * @returns Statistics object with counts
 *
 * @example
 * ```typescript
 * const stats = await getBookingStats();
 * console.log(`${stats.pending} pending out of ${stats.total} total`);
 * console.log(`Confirmation rate: ${(stats.confirmed / stats.total * 100).toFixed(1)}%`);
 * ```
 */
export async function getBookingStats(): Promise<BookingStats> {
  return callMCPTool<BookingStats>("database__get_booking_stats", {});
}

// Export all functions
export default {
  getBookings,
  getBookingById,
  getBookingStats,
};
