// ===== POS Terminal =====
export interface PosTerminal {
  id: number;
  name: string;
  name_ar: string;
  code: string;
  is_active: boolean;
  settings: PosTerminalSettings;
  branch: { id: number; name: string };
  warehouse: { id: number; name: string };
  created_at: string;
  updated_at: string;
}

export interface PosTerminalSettings {
  allow_negative_stock: boolean;
  require_customer: boolean;
  default_tax_rate: number | null;
  receipt_header: string;
  receipt_footer: string;
  allow_discount: boolean;
  allow_price_override: boolean;
}

// ===== POS Session =====
export interface PosSession {
  id: number;
  terminal_id?: number;
  user_id?: number;
  branch_id?: number;
  warehouse_id?: number;
  opening_balance: string;
  closing_balance: string;
  expected_balance: string;
  status: { value: string; label: string };
  opened_at: string;
  closed_at: string | null;
  closing_notes: string | null;
  terminal: { id: number; name: string; code: string };
  user: { id: number; name: string };
  branch: { id: number; name: string };
  warehouse: { id: number; name: string };
  created_at: string;
  updated_at: string;
}

export interface SessionPaymentSummary {
  method: string;
  expected_amount: number;
  actual_amount: number;
}

export interface OpenSessionRequest {
  terminal_id: number;
  opening_balance: number;
}

export interface CloseSessionRequest {
  payments: SessionPaymentSummary[];
  closing_notes: string;
}

// ===== Cart =====
export interface CartItem {
  product_id: number;
  variant_id: number | null;
  name: string;
  barcode: string;
  qty: number;
  unit_price: number;
  discount_percent: number;
  discount_amount: number;
  tax_rate: number;
  tax_amount: number;
  line_total: number;
  product: PosProduct;
}

export interface Cart {
  items: CartItem[];
  customer_id: number | null;
  coupon: CouponValidation | null;
  subtotal: number;
  tax_amount: number;
  discount_amount: number;
  total: number;
}

// ===== POS Product =====
export interface PosProduct {
  id: number;
  code: string;
  sku: string;
  barcode: string;
  name: string;
  name_en: string;
  name_ar: string;
  sale_price: string;
  min_sale_price: string;
  image: string | null;
  is_active: boolean;
  track_inventory: boolean;
  category: { id: number; name: string } | null;
  base_unit: { id: number; name: string; abbreviation: string | null } | null;
  stock: PosProductStock | null;
  has_variants?: boolean;
  variants?: PosProductVariant[];
}

export interface PosProductVariant {
  id: number;
  name: string;
  barcode: string;
  sale_price: string;
  stock_quantity: number;
}

export interface PosProductStock {
  total_quantity: number;
  total_reserved: number;
  total_available: number;
  warehouses: {
    warehouse_id: number;
    quantity: string;
    reserved: string;
    available: number;
  }[];
}

// ===== Payment =====
export type PaymentMethod = 'cash' | 'credit_card' | 'bank_transfer' | 'check';

export interface PaymentLine {
  method: PaymentMethod;
  amount: number;
  reference?: string;
}

export interface PaymentRequest {
  invoice_id: number;
  payments: PaymentLine[];
}

// ===== Return =====
export interface ReturnItem {
  invoice_item_id: number;
  product_id: number;
  return_qty: number;
  reason: string;
}

export interface ReturnRequest {
  invoice_id: number;
  items: ReturnItem[];
  refund_method: PaymentMethod;
  session_id: number;
}

// ===== Held Orders =====
export interface HeldOrder {
  id: number;
  session_id: number;
  customer_id: number | null;
  customer_name?: string;
  items: CartItem[];
  subtotal: number;
  total: number;
  note: string;
  created_at: string;
}

// ===== Coupon =====
export interface Coupon {
  id: number;
  code: string;
  type: 'percentage' | 'fixed';
  value: number;
  max_discount: number | null;
  min_order_amount: number | null;
  usage_limit: number | null;
  usage_count: number;
  is_active: boolean;
  valid_from: string;
  valid_to: string;
  created_at?: string;
  updated_at?: string;
}

export interface CouponValidation {
  valid: boolean;
  discount_type: 'percentage' | 'fixed';
  discount_value: number;
  discount_amount: number;
  message: string;
}

// ===== Offline / Sync =====
export type ConnectionStatus = 'online' | 'pending' | 'offline';

export interface SyncQueueItem {
  id?: number;
  type: 'sale' | 'invoice' | 'payment' | 'return';
  payload: any;
  created_at: string;
  retries: number;
  local_id?: string; // UUID for dedup
}

// ===== Product Category (POS view) =====
export interface PosCategory {
  id: number;
  name: string;
  name_ar: string;
  name_en: string;
  parent_id: number | null;
  products_count?: number;
}
