/**
 * AI Assistant Service
 * Handles communication with the AI assistant backend
 */

import apiClient from '../../../shared/services/apiClient';

export interface AIChatRequest {
  message: string;
  context?: Record<string, unknown>;
}

export interface AIChatResponse {
  response: string;
  intent: string;
  data_used: Record<string, unknown>;
  timestamp: string;
  user_role?: string; // Added: user role information
}

export interface SystemStatus {
  user_role?: string; // Added: user role information
  timestamp: string;
  room_status: {
    total_rooms: number;
    available: number;
    occupied: number;
    maintenance?: number;
    cleaning?: number;
    occupancy_rate: number;
    rooms_with_active_bookings?: number;
  };
  booking_summary?: {
    upcoming_checkins: number;
    upcoming_checkouts: number;
    active_bookings: number;
    pending_bookings: number;
  };
  user_bookings?: Array<{
    booking_number: string;
    status: string;
    check_in: string;
    check_out: string;
    total_price: number;
    room_number?: string;
    room_type?: string;
  }>;
  payment_summary?: {
    total_revenue: number;
    pending_amount: number;
    completed_payments: number;
  };
  invoice_summary?: {
    total_invoices: number;
    overdue_invoices: number;
    status_breakdown?: Record<string, number>;
  };
  user_invoices?: Array<{
    invoice_number: string;
    status: string;
    total_amount: number;
    balance_due: number;
    due_date: string;
    booking_number?: string;
  }>;
  user_payments?: Array<{
    id: number;
    amount: number;
    payment_method: string;
    payment_status: string;
    payment_date: string;
    booking_number?: string;
  }>;
  unanswered_chats?: Array<{
    chat_id: number;
    visitor_name: string;
    last_message: string;
    waiting_hours: number;
  }>;
  room_problems?: Array<{
    room_number: string;
    issue_type: string;
    description: string;
  }>;
  application_knowledge?: {
    features: string[];
    role_info: Record<string, unknown>;
  };
}

export interface OccupiedRoom {
  room_number: string;
  room_type: string;
  floor: number;
  booking_id: number;
  guest_name: string;
  check_in: string;
  check_out: string;
}

/**
 * Send a message to the AI assistant
 */
export const chatWithAI = async (
  message: string,
  context?: Record<string, unknown>
): Promise<AIChatResponse> => {
  const response = await apiClient.post<{ status: string; data: AIChatResponse }>(
    '/ai-assistant/chat',
    { message, context }
  );
  return response.data.data;
};

/**
 * Get comprehensive system status
 */
export const getAIStatus = async (): Promise<SystemStatus> => {
  const response = await apiClient.get<{ status: string; data: SystemStatus }>(
    '/ai-assistant/status'
  );
  return response.data.data;
};

/**
 * Get list of occupied rooms
 */
export const getOccupiedRooms = async (
  roomNumber?: string
): Promise<OccupiedRoom[]> => {
  const response = await apiClient.get<{ status: string; data: { rooms: OccupiedRoom[] } }>(
    '/ai-assistant/rooms/occupied',
    { params: roomNumber ? { room_number: roomNumber } : {} }
  );
  return response.data.data.rooms;
};

/**
 * Get rooms with problems
 */
export const getRoomProblems = async (): Promise<Array<{
  room_number: string;
  issue_type: string;
  description: string;
}>> => {
  const response = await apiClient.get<{ status: string; data: { problems: Array<{
    room_number: string;
    issue_type: string;
    description: string;
  }> } }>(
    '/ai-assistant/rooms/problems'
  );
  return response.data.data.problems;
};

/**
 * Get unanswered chat messages
 */
export const getUnansweredChats = async (
  hours: number = 24
): Promise<Array<{
  chat_id: number;
  visitor_name: string;
  last_message: string;
  waiting_hours: number;
}>> => {
  const response = await apiClient.get<{ status: string; data: { chats: Array<{
    chat_id: number;
    visitor_name: string;
    last_message: string;
    waiting_hours: number;
  }> } }>(
    '/ai-assistant/chats/unanswered',
    { params: { hours } }
  );
  return response.data.data.chats;
};

