export type UserRole = 'admin' | 'staff' | 'technician' | 'accounts';

export type OrderStatus =
  | 'draft'
  | 'in_progress'
  | 'waiting_on_parts'
  | 'ready'
  | 'overdue'
  | 'paid'
  | 'closed';

export type LineItemKind = 'part' | 'labor' | 'custom';

export type PaymentMethod = 'card' | 'cash' | 'invoice' | 'bank_transfer';

export type ReceiptStatus = 'paid' | 'refunded';

export type Tier = 'none' | 'silver' | 'gold' | 'bronze';

export type AppointmentStatus = 'pending' | 'confirmed' | 'completed' | 'cancelled' | 'converted';

export type ApprovalStatus = 'none' | 'pending' | 'approved' | 'declined';

export interface Shop {
  id: string;
  name: string;
  address: string;
  phone: string;
  email: string;
  taxRate: number;
  laborRate: number;
  openHour: number;
  closeHour: number;
  slotCapacity: number;
  updatedAt: string;
}

export interface User {
  id: string;
  name: string;
  email: string;
  role: UserRole;
  phone?: string | null;
  avatarUrl?: string | null;
  active: boolean;
  createdAt: string;
}

export interface Customer {
  id: string;
  name: string;
  phone: string;
  email?: string | null;
  address?: string | null;
  notes?: string | null;
  tier: Tier;
  portalToken?: string | null;
  createdAt: string;
  vehicles?: Vehicle[];
  orders?: WorkOrder[];
}

export interface Vehicle {
  id: string;
  customerId: string;
  customer?: Customer;
  year: number;
  make: string;
  model: string;
  plate: string;
  vin?: string | null;
  miles: number;
  color?: string | null;
  orders?: WorkOrder[];
}

export interface WorkOrder {
  id: string;
  no: string;
  customerId: string;
  customer?: Customer;
  vehicleId: string;
  vehicle?: Vehicle;
  assignedToId?: string | null;
  assignedTo?: User | null;
  status: OrderStatus;
  mileageIn: number;
  notes?: string | null;
  taxRate: number;
  discount: number;
  approvalStatus: ApprovalStatus;
  approvalAt?: string | null;
  declineReason?: string | null;
  createdAt: string;
  updatedAt: string;
  items?: LineItem[];
  receipt?: Receipt | null;
}

export interface LineItem {
  id: string;
  orderId: string;
  kind: LineItemKind;
  description: string;
  qty: number;
  unitPrice: number;
  inventoryItemId?: string | null;
  inventoryItem?: InventoryItem | null;
}

export interface InventoryItem {
  id: string;
  sku: string;
  name: string;
  category: string;
  stock: number;
  reorderLevel: number;
  cost: number;
  price: number;
}

export interface Receipt {
  id: string;
  no: string;
  orderId: string;
  order?: WorkOrder;
  date: string;
  method: PaymentMethod;
  status: ReceiptStatus;
  subtotal: number;
  discount: number;
  tax: number;
  total: number;
}

export interface ServiceCatalogItem {
  id: string;
  slug: string;
  name: string;
  description: string;
  category: string;
  price: number;
  durationMin: number;
  active: boolean;
  sortOrder: number;
}

export interface Appointment {
  id: string;
  no: string;
  serviceId: string;
  service?: ServiceCatalogItem;
  customerId?: string | null;
  customer?: Customer | null;
  vehicleId?: string | null;
  vehicle?: Vehicle | null;
  name: string;
  phone: string;
  email?: string | null;
  vehicleYear?: number | null;
  vehicleMake?: string | null;
  vehicleModel?: string | null;
  notes?: string | null;
  internalNotes?: string | null;
  date: string;
  slot: string;
  durationMin: number;
  status: AppointmentStatus;
  cancelReason?: string | null;
  convertedOrderId?: string | null;
  convertedOrder?: WorkOrder | null;
  createdAt: string;
  updatedAt: string;
}

export interface AuditLog {
  id: string;
  userId?: string | null;
  user?: User | null;
  action: string;
  entityType: string;
  entityId: string;
  details?: unknown;
  createdAt: string;
}

export interface OrderTotals {
  parts: number;
  labor: number;
  custom: number;
  subtotal: number;
  discount: number;
  tax: number;
  total: number;
}

export interface PaginatedResponse<T> {
  data: T[];
  nextCursor: string | null;
  total: number;
}

export interface ApiError {
  error: string;
  code?: string;
}
