/**
 * TouchBistro MCP Server - Complete Type Definitions
 * Full production-grade types for restaurant POS operations
 */

// ============================================================================
// Core Domain Types
// ============================================================================

export interface TouchBistroConfig {
  apiKey: string;
  clientId: string;
  clientSecret: string;
  restaurantId: string;
  baseUrl?: string;
  sandbox?: boolean;
}

// ============================================================================
// Menu Management
// ============================================================================

export interface MenuItem {
  id: string;
  name: string;
  description?: string;
  price: number;
  cost?: number;
  categoryId: string;
  sku?: string;
  barcode?: string;
  taxable: boolean;
  available: boolean;
  prepTime?: number; // minutes
  modifierGroupIds?: string[];
  allergens?: string[];
  nutritionInfo?: NutritionInfo;
  image?: string;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
}

export interface MenuCategory {
  id: string;
  name: string;
  description?: string;
  parentCategoryId?: string;
  sortOrder: number;
  available: boolean;
  menuIds: string[];
  itemCount: number;
  createdAt: string;
  updatedAt: string;
}

export interface Menu {
  id: string;
  name: string;
  description?: string;
  startTime?: string; // HH:mm
  endTime?: string; // HH:mm
  daysOfWeek?: number[]; // 0-6
  available: boolean;
  categoryIds: string[];
  createdAt: string;
  updatedAt: string;
}

export interface ModifierGroup {
  id: string;
  name: string;
  minSelections: number;
  maxSelections: number;
  required: boolean;
  modifiers: Modifier[];
  sortOrder: number;
}

export interface Modifier {
  id: string;
  name: string;
  price: number;
  available: boolean;
  sortOrder: number;
}

export interface NutritionInfo {
  calories?: number;
  protein?: number;
  carbohydrates?: number;
  fat?: number;
  fiber?: number;
  sodium?: number;
}

// ============================================================================
// Order Management
// ============================================================================

export interface Order {
  id: string;
  orderNumber: string;
  status: OrderStatus;
  type: OrderType;
  tableId?: string;
  customerId?: string;
  items: OrderItem[];
  subtotal: number;
  tax: number;
  tip: number;
  discount: number;
  total: number;
  payments: Payment[];
  serverId: string;
  guestCount: number;
  notes?: string;
  specialInstructions?: string;
  sentToKitchen: boolean;
  kitchenSentAt?: string;
  createdAt: string;
  updatedAt: string;
  completedAt?: string;
  voidedAt?: string;
  voidReason?: string;
}

export enum OrderStatus {
  Draft = 'draft',
  Pending = 'pending',
  InProgress = 'in_progress',
  Ready = 'ready',
  Completed = 'completed',
  Voided = 'voided',
  Cancelled = 'cancelled'
}

export enum OrderType {
  DineIn = 'dine_in',
  Takeout = 'takeout',
  Delivery = 'delivery',
  Curbside = 'curbside',
  DriveThru = 'drive_thru'
}

export interface OrderItem {
  id: string;
  menuItemId: string;
  name: string;
  quantity: number;
  price: number;
  modifiers: OrderModifier[];
  specialInstructions?: string;
  sentToKitchen: boolean;
  preparedBy?: string;
  preparedAt?: string;
  seat?: number;
  courseNumber?: number;
}

export interface OrderModifier {
  id: string;
  name: string;
  price: number;
}

// ============================================================================
// Payment Processing
// ============================================================================

export interface Payment {
  id: string;
  orderId: string;
  type: PaymentType;
  method: PaymentMethod;
  amount: number;
  tip: number;
  processorFee?: number;
  status: PaymentStatus;
  cardLast4?: string;
  cardBrand?: string;
  transactionId?: string;
  authCode?: string;
  processedAt?: string;
  refundedAt?: string;
  refundAmount?: number;
  createdAt: string;
}

export enum PaymentType {
  Full = 'full',
  Partial = 'partial',
  Split = 'split',
  Refund = 'refund'
}

export enum PaymentMethod {
  Cash = 'cash',
  CreditCard = 'credit_card',
  DebitCard = 'debit_card',
  GiftCard = 'gift_card',
  MobilePayment = 'mobile_payment',
  Check = 'check',
  HouseAccount = 'house_account'
}

export enum PaymentStatus {
  Pending = 'pending',
  Authorized = 'authorized',
  Captured = 'captured',
  Declined = 'declined',
  Refunded = 'refunded',
  Voided = 'voided'
}

// ============================================================================
// Table & Floor Management
// ============================================================================

export interface Table {
  id: string;
  number: string;
  name?: string;
  sectionId: string;
  capacity: number;
  shape: TableShape;
  status: TableStatus;
  currentOrderId?: string;
  serverId?: string;
  guestCount?: number;
  seatedAt?: string;
  positionX: number;
  positionY: number;
  rotation?: number;
}

export enum TableShape {
  Square = 'square',
  Rectangle = 'rectangle',
  Circle = 'circle',
  Oval = 'oval'
}

export enum TableStatus {
  Available = 'available',
  Occupied = 'occupied',
  Reserved = 'reserved',
  Cleaning = 'cleaning',
  OutOfService = 'out_of_service'
}

export interface Section {
  id: string;
  name: string;
  floorPlanId: string;
  color?: string;
  assignedServerIds: string[];
  tableCount: number;
}

export interface FloorPlan {
  id: string;
  name: string;
  active: boolean;
  sections: Section[];
  tableCount: number;
  createdAt: string;
  updatedAt: string;
}

// ============================================================================
// Staff Management
// ============================================================================

export interface Staff {
  id: string;
  employeeNumber: string;
  firstName: string;
  lastName: string;
  email?: string;
  phone?: string;
  role: StaffRole;
  pinCode: string;
  active: boolean;
  hireDate: string;
  terminationDate?: string;
  wage?: number;
  wageType?: WageType;
  permissions: StaffPermission[];
  assignedSections?: string[];
  createdAt: string;
  updatedAt: string;
}

export enum StaffRole {
  Owner = 'owner',
  Manager = 'manager',
  Server = 'server',
  Bartender = 'bartender',
  Host = 'host',
  Chef = 'chef',
  Cook = 'cook',
  Busser = 'busser',
  Cashier = 'cashier'
}

export enum WageType {
  Hourly = 'hourly',
  Salary = 'salary',
  TippedHourly = 'tipped_hourly'
}

export interface StaffPermission {
  resource: string;
  actions: ('create' | 'read' | 'update' | 'delete' | 'void')[];
}

export interface TimeSheet {
  id: string;
  staffId: string;
  clockIn: string;
  clockOut?: string;
  breakMinutes: number;
  totalHours?: number;
  wage: number;
  tips: number;
  date: string;
  notes?: string;
}

// ============================================================================
// Customer Management
// ============================================================================

export interface Customer {
  id: string;
  firstName: string;
  lastName: string;
  email?: string;
  phone?: string;
  birthday?: string;
  notes?: string;
  loyaltyPoints: number;
  loyaltyTier?: string;
  totalSpent: number;
  visitCount: number;
  averageSpend: number;
  lastVisit?: string;
  preferredServer?: string;
  dietaryRestrictions?: string[];
  allergies?: string[];
  tags?: string[];
  createdAt: string;
  updatedAt: string;
}

export interface LoyaltyTransaction {
  id: string;
  customerId: string;
  orderId?: string;
  points: number;
  type: 'earned' | 'redeemed' | 'adjusted';
  description: string;
  createdAt: string;
}

// ============================================================================
// Reservations
// ============================================================================

export interface Reservation {
  id: string;
  customerId?: string;
  customerName: string;
  customerPhone?: string;
  customerEmail?: string;
  partySize: number;
  date: string;
  time: string;
  duration: number; // minutes
  status: ReservationStatus;
  tableId?: string;
  sectionId?: string;
  notes?: string;
  specialRequests?: string;
  confirmationSent: boolean;
  reminderSent: boolean;
  seatedAt?: string;
  completedAt?: string;
  noShowAt?: string;
  cancelledAt?: string;
  createdAt: string;
  updatedAt: string;
}

export enum ReservationStatus {
  Pending = 'pending',
  Confirmed = 'confirmed',
  Seated = 'seated',
  Completed = 'completed',
  NoShow = 'no_show',
  Cancelled = 'cancelled'
}

// ============================================================================
// Inventory Management
// ============================================================================

export interface InventoryItem {
  id: string;
  name: string;
  category: string;
  unit: string;
  currentStock: number;
  parLevel: number;
  reorderPoint: number;
  reorderQuantity: number;
  unitCost: number;
  supplierId?: string;
  sku?: string;
  barcode?: string;
  location?: string;
  expirationDate?: string;
  lastRestocked?: string;
  createdAt: string;
  updatedAt: string;
}

export interface StockAdjustment {
  id: string;
  inventoryItemId: string;
  quantity: number;
  type: 'addition' | 'subtraction' | 'correction';
  reason: string;
  performedBy: string;
  cost?: number;
  createdAt: string;
}

export interface Supplier {
  id: string;
  name: string;
  contactName?: string;
  email?: string;
  phone?: string;
  address?: string;
  paymentTerms?: string;
  notes?: string;
  active: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface PurchaseOrder {
  id: string;
  supplierId: string;
  orderNumber: string;
  status: 'draft' | 'sent' | 'received' | 'cancelled';
  items: PurchaseOrderItem[];
  subtotal: number;
  tax: number;
  total: number;
  expectedDate?: string;
  receivedDate?: string;
  notes?: string;
  createdAt: string;
  updatedAt: string;
}

export interface PurchaseOrderItem {
  inventoryItemId: string;
  quantity: number;
  unitCost: number;
  total: number;
}

// ============================================================================
// Reporting & Analytics
// ============================================================================

export interface SalesReport {
  startDate: string;
  endDate: string;
  totalSales: number;
  totalOrders: number;
  averageOrderValue: number;
  totalTax: number;
  totalTips: number;
  totalDiscounts: number;
  grossSales: number;
  netSales: number;
  salesByHour: HourlySales[];
  salesByCategory: CategorySales[];
  salesByMenuItem: MenuItemSales[];
  salesByPaymentMethod: PaymentMethodSales[];
  salesByServer: ServerSales[];
  orderTypeMix: OrderTypeMix[];
}

export interface HourlySales {
  hour: number;
  sales: number;
  orders: number;
  averageValue: number;
}

export interface CategorySales {
  categoryId: string;
  categoryName: string;
  quantity: number;
  revenue: number;
  percentage: number;
}

export interface MenuItemSales {
  itemId: string;
  itemName: string;
  quantity: number;
  revenue: number;
  cost?: number;
  profit?: number;
  margin?: number;
}

export interface PaymentMethodSales {
  method: PaymentMethod;
  count: number;
  total: number;
  percentage: number;
}

export interface ServerSales {
  serverId: string;
  serverName: string;
  orders: number;
  sales: number;
  tips: number;
  averageCheck: number;
}

export interface OrderTypeMix {
  type: OrderType;
  count: number;
  revenue: number;
  percentage: number;
}

export interface InventoryReport {
  totalValue: number;
  lowStockItems: number;
  outOfStockItems: number;
  expiringItems: number;
  itemsByCategory: {
    category: string;
    count: number;
    value: number;
  }[];
}

export interface StaffReport {
  totalHours: number;
  totalWages: number;
  totalTips: number;
  laborCostPercentage: number;
  staffPerformance: StaffPerformance[];
}

export interface StaffPerformance {
  staffId: string;
  staffName: string;
  hours: number;
  sales: number;
  ordersServed: number;
  salesPerHour: number;
  averageCheck: number;
}

// ============================================================================
// API Response Types
// ============================================================================

export interface ApiResponse<T> {
  success: boolean;
  data?: T;
  error?: ApiError;
  metadata?: ResponseMetadata;
}

export interface ApiError {
  code: string;
  message: string;
  details?: Record<string, any>;
}

export interface ResponseMetadata {
  page?: number;
  pageSize?: number;
  totalCount?: number;
  totalPages?: number;
  timestamp: string;
}

export interface ListParams {
  page?: number;
  pageSize?: number;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
  filter?: Record<string, any>;
}

// ============================================================================
// MCP App State Types
// ============================================================================

export interface AppState {
  loading: boolean;
  error: string | null;
  lastUpdated: string | null;
}

export interface OrdersAppState extends AppState {
  orders: Order[];
  selectedOrder: Order | null;
  filters: OrderFilters;
}

export interface OrderFilters {
  status?: OrderStatus[];
  type?: OrderType[];
  serverId?: string;
  dateFrom?: string;
  dateTo?: string;
}

export interface MenuAppState extends AppState {
  items: MenuItem[];
  categories: MenuCategory[];
  menus: Menu[];
  selectedItem: MenuItem | null;
  editMode: boolean;
}

export interface TablesAppState extends AppState {
  tables: Table[];
  sections: Section[];
  selectedTable: Table | null;
  floorPlan: FloorPlan | null;
}

export interface StaffAppState extends AppState {
  staff: Staff[];
  selectedStaff: Staff | null;
  timesheets: TimeSheet[];
}

export interface CustomersAppState extends AppState {
  customers: Customer[];
  selectedCustomer: Customer | null;
  searchQuery: string;
}

export interface ReservationsAppState extends AppState {
  reservations: Reservation[];
  selectedReservation: Reservation | null;
  filters: ReservationFilters;
}

export interface ReservationFilters {
  status?: ReservationStatus[];
  dateFrom?: string;
  dateTo?: string;
}

export interface InventoryAppState extends AppState {
  items: InventoryItem[];
  lowStockItems: InventoryItem[];
  suppliers: Supplier[];
  selectedItem: InventoryItem | null;
}

export interface ReportsAppState extends AppState {
  salesReport: SalesReport | null;
  inventoryReport: InventoryReport | null;
  staffReport: StaffReport | null;
  dateRange: { start: string; end: string };
}
