// ============================================================
// LasminiShop - Type Definitions
// ============================================================

export interface User {
  id: number;
  name: string;
  email: string;
  role: 'admin' | 'manager' | 'kasir' | 'gudang';
  tenant_id?: number;
}

export interface AuthResponse {
  user: User;
  /** Present only for legacy bearer clients; SPA uses session cookie */
  token?: string;
  token_type?: string;
  auth_mode?: 'session' | 'token';
}

export interface Category {
  id: number;
  name: string;
  slug: string;
  description?: string;
  is_active?: boolean;
  products?: Product[];
}

// ============================================================
// Customer
// ============================================================
export interface Customer {
  id: number;
  name: string;
  phone?: string;
  email?: string;
  address?: string;
  credit_limit?: number;
  outstanding_balance?: number;
  is_active: boolean;
  created_at?: string;
}

// ============================================================
// Product (Grosir model - no addons, no station)
// ============================================================
export interface PriceTier {
  id: number;
  product_id: number;
  min_qty: number;
  price: number;
  label?: string;
}

/** Sale unit conversion (1 unit = multiplier base stock units) */
export interface ProductUnit {
  id: number;
  product_id: number;
  name: string;
  abbreviation?: string | null;
  multiplier: number;
  is_default_sale?: boolean;
  sort_order?: number;
}

export interface Product {
  id: number;
  category_id: number;
  name: string;
  slug?: string;
  description?: string;
  price: number;
  image?: string;
  is_available: boolean;
  unit: string;
  barcode?: string;
  sku?: string;
  stock: number;
  min_stock: number;
  min_order_qty?: number;
  order_step?: number;
  cost_per_unit?: number;
  supplier_id?: number;
  category?: Category;
  prices?: PriceTier[];
  units?: ProductUnit[];
}

// ============================================================
// Order (Grosir model - no queue, no dine-in/walk-in)
// ============================================================
export interface OrderItem {
  id?: number;
  product_id: number;
  product_unit_id?: number | null;
  product_name: string;
  product_price: number;
  /** Base stock qty (for display after save) or sale qty in cart */
  quantity: number;
  unit_qty?: number | null;
  unit_name?: string | null;
  unit?: string;
  unit_price: number;
  discount?: number;
  subtotal: number;
  notes?: string;
}

export interface Order {
  id: number;
  order_number: string;
  user_id?: number;
  customer_id?: number;
  customer_name?: string;
  customer?: Customer;
  status: 'pending' | 'processing' | 'completed' | 'cancelled' | 'voided' | 'refunded';
  payment_method: 'cash' | 'transfer' | 'qris';
  payment_status: 'unpaid' | 'paid' | 'partial';
  due_date?: string;
  subtotal: number;
  tax: number;
  discount: number;
  total: number;
  paid_amount?: number;
  amount_paid?: number;
  balance_due?: number;
  cash_received?: number;
  cash_change?: number;
  refund_amount?: number;
  refund_reason?: string;
  refunded_at?: string;
  void_reason?: string;
  voided_at?: string;
  items: OrderItem[];
  user?: User;
  created_at: string;
}

// ============================================================
// Expense
// ============================================================
export interface Expense {
  id: number;
  category: string;
  description: string;
  amount: number;
  date: string;
  notes?: string;
}

// ============================================================
// Dashboard
// ============================================================
export interface DashboardStats {
  today_revenue: number;
  yesterday_revenue: number;
  today_orders: number;
  yesterday_orders: number;
  monthly_revenue: number;
  last_month_revenue: number;
  /** Admin/manager only — omitted for kasir */
  today_profit?: number;
  /** Admin/manager only — omitted for kasir */
  monthly_net_profit?: number;
  total_products: number;
  low_stock_count: number;
  avg_order_value: number;
  receivable_total?: number;
  receivable_count?: number;
}

export interface TopProduct {
  product_id: number;
  product_name: string;
  total_qty: number;
  total_revenue: number;
  order_count: number;
}

export interface PeakHour {
  hour: number;
  label: string;
  order_count: number;
  revenue: number;
}

export interface SalesByCategory {
  category_id: number;
  category_name: string;
  total_qty: number;
  total_revenue: number;
}

export interface SalesByPayment {
  method: string;
  label: string;
  order_count: number;
  total_revenue: number;
}

// ============================================================
// Held Order (Open Bill) - Grosir model
// ============================================================
export interface HeldOrder {
  id: number;
  label?: string;
  cart_data: CartItemData[];
  customer_id?: number;
  customer_name?: string;
  payment_method: 'cash' | 'transfer' | 'qris';
  discount: number;
  created_at: string;
}

export interface CartItemData {
  product_id: number;
  product_unit_id?: number | null;
  product_name: string;
  product_price: number;
  unit: string;
  unit_name?: string | null;
  /** Sale-unit quantity (what cashier entered) */
  quantity: number;
  /** Base stock qty for stock checks */
  base_qty?: number;
  multiplier?: number;
  unit_price: number;
  discount?: number;
  selected_discount_id?: number;
  subtotal: number;
  notes?: string;
  is_edited?: boolean;
}

// ============================================================
// Discount
// ============================================================
export interface AvailableDiscount {
  id: number;
  name: string;
  code: string | null;
  type: 'percentage' | 'fixed';
  value: number;
  scope: 'all' | 'order' | 'category' | 'product';
  category_id: number | null;
  category_name: string | null;
  product_id: number | null;
  product_name: string | null;
  min_order: number;
  max_discount: number | null;
  label: string;
}

// ============================================================
// Receipt
// ============================================================
export interface ReceiptItem {
  id: number;
  product_id: number;
  product_name: string;
  product_price: number;
  quantity: number;
  unit_qty?: number | null;
  unit_name?: string | null;
  unit: string;
  unit_price: number;
  subtotal: number;
  notes?: string;
  category_name?: string;
}

export interface ReceiptData {
  id: number;
  order_number: string;
  customer_name?: string;
  customer?: Customer;
  status: string;
  payment_method: string;
  payment_status: string;
  subtotal: number;
  tax: number;
  tax_rate?: number;
  discount: number;
  discount_label?: string;
  total: number;
  paid_amount?: number;
  cash_received?: number;
  cash_change?: number;
  due_date?: string;
  cashier_name: string;
  created_at: string;
  items: ReceiptItem[];
}

// ============================================================
// Request/Payload Types
// ============================================================
export interface CreateOrderPayload {
  customer_id?: number;
  customer_name?: string;
  /** Kasir ecer always sends retail; partai uses Filament admin */
  order_type?: 'retail' | 'wholesale' | 'walk-in' | 'dine-in';
  payment_method: 'cash' | 'transfer' | 'qris';
  payment_status?: 'paid' | 'unpaid' | 'partial';
  due_date?: string;
  paid_amount?: number;
  /** Required for transfer/QRIS when payment_status=paid */
  payment_reference?: string;
  discount?: number;
  discount_id?: number;
  cash_received?: number;
  notes?: string;
  items: {
    product_id: number;
    quantity: number;
    product_unit_id?: number | null;
    unit_price?: number;
    discount?: number;
    notes?: string;
  }[];
}

export interface CreateExpensePayload {
  category: string;
  description: string;
  amount: number;
  date: string;
  notes?: string;
}

export interface HoldOrderPayload {
  label?: string;
  cart_data: CartItemData[];
  customer_id?: number;
  customer_name?: string;
  payment_method?: string;
  discount?: number;
}

export interface PaginatedResponse<T> {
  data: T[];
  total: number;
  current_page: number;
  last_page: number;
  per_page: number;
}

// ============================================================
// Notification Types
// ============================================================
export interface AppNotification {
  id: number;
  type: string;
  title: string;
  message: string;
  icon?: string;
  color: 'info' | 'success' | 'warning' | 'danger';
  link?: string;
  data?: Record<string, unknown>;
  is_read: boolean;
  user_id?: number;
  created_at: string;
}

export interface NotificationResponse {
  notifications: AppNotification[];
  unread_count: number;
}

// ============================================================
// Settings Types
// ============================================================
export interface StoreSettings {
  store_name: string;
  store_address: string;
  store_phone: string;
  store_email: string;
  store_logo?: string;
  tax_enabled: boolean;
  tax_rate: number;
  tax_name: string;
  tax_included: boolean;
  receipt_header: string;
  receipt_footer: string;
  receipt_show_logo: boolean;
  low_stock_alert: boolean;
  new_order_sound: boolean;
}
