export type PosPaymentMode = 'cash' | 'credit_card' | 'debit_card' | 'ewallet';

export type PosPaymentLine = {
  payment_mode: PosPaymentMode;
  amount: number;
  capture_mode: 'manual' | 'device';
  card_brand?: string;
  card_last4?: string;
  authorization_code?: string;
  reference_number?: string;
  ewallet_provider?: string;
  ewallet_account?: string;
  device_transaction_id?: string;
  payment_notes?: string;
};

export type PosPaymentDevice = {
  id: number;
  name: string;
  device_type?: string;
  provider?: string;
  serial_number?: string;
  connection_status: 'disconnected' | 'connected' | 'syncing';
  status?: string;
};

export type PosSession = {
  id: number;
  connected_device_id?: number | null;
  connected_device?: PosPaymentDevice | null;
  register?: { name?: string } | null;
};

export type PosSaleLine = {
  id: number;
  name: string;
  quantity: number;
  quantity_returned?: number;
  unit_price: number | string;
  line_total: number | string;
  service_product_id?: number | null;
};

export type PosSalePayment = PosPaymentLine & {
  id: number;
};

export type PosCustomerOption = {
  value: number;
  label: string;
};

export type PosPatientOption = {
  value: number;
  label: string;
  phone?: string;
  email?: string;
};

export type PosReconciliation = {
  session_id?: number;
  status?: string;
  opening_float?: number;
  expected_cash?: number;
  closing_cash?: number | null;
  cash_variance?: number | null;
  cash_sales?: number;
  card_sales?: number;
  ewallet_sales?: number;
  sales_count?: number;
  gross_sales?: number;
  total_refunds?: number;
  net_sales?: number;
};

export type PosSale = {
  id: number;
  order_number: string;
  total: number | string;
  subtotal?: number | string;
  discount?: number | string;
  tax_amount?: number | string;
  status: string;
  customer_id?: number | null;
  patient_id?: number | null;
  customer_type?: 'walk_in' | 'customer';
  customer_display?: string;
  patient_display?: string;
  notes?: string | null;
  payments?: PosSalePayment[];
  lines?: PosSaleLine[];
};

export type PosReceiptData = {
  order: PosSale;
  lines: PosSaleLine[];
  payments: PosSalePayment[];
  printed_at?: string;
};

export const POS_PAYMENT_MODES: { value: PosPaymentMode; label: string }[] = [
  { value: 'cash', label: 'Cash' },
  { value: 'credit_card', label: 'Credit card' },
  { value: 'debit_card', label: 'Debit card' },
  { value: 'ewallet', label: 'E-wallet' },
];

export function createPaymentLine(mode: PosPaymentMode = 'cash', amount = 0): PosPaymentLine {
  return {
    payment_mode: mode,
    amount,
    capture_mode: 'manual',
    card_brand: '',
    card_last4: '',
    authorization_code: '',
    reference_number: '',
    ewallet_provider: '',
    ewallet_account: '',
    device_transaction_id: '',
    payment_notes: '',
  };
}

export function isElectronicPayment(mode: PosPaymentMode) {
  return mode === 'credit_card' || mode === 'debit_card' || mode === 'ewallet';
}
