// Defines interfaces related to Expenses, Splits, and Settlement Activities
import type { UserPublic } from './user'

// Enums for statuses - align these string values with your backend enums
export enum ExpenseSplitStatusEnum {
  UNPAID = 'unpaid',
  PARTIALLY_PAID = 'partially_paid',
  PAID = 'paid',
}

export enum ExpenseOverallStatusEnum {
  UNPAID = 'unpaid',
  PARTIALLY_PAID = 'partially_paid',
  PAID = 'paid',
}

// For creating a new settlement activity via API
export interface SettlementActivityCreate {
  expense_split_id: number
  paid_by_user_id: number
  amount_paid: string // String representation of Decimal for API payload
  paid_at?: string // ISO datetime string, optional, backend can default to now()
}

export interface SettlementActivity {
  id: number
  expense_split_id: number
  paid_by_user_id: number
  paid_at: string // ISO datetime string
  amount_paid: string // String representation of Decimal
  created_by_user_id: number
  created_at: string // ISO datetime string
  updated_at: string // ISO datetime string
  version: number
  payer?: UserPublic | null
  creator?: UserPublic | null
}

// Type alias for consistency with backend response
export type SettlementActivityPublic = SettlementActivity

// Settlement types for generic settlements
export interface SettlementCreate {
  group_id: number
  paid_by_user_id: number
  paid_to_user_id: number
  amount: string
  settlement_date?: string
  description?: string
}

export interface SettlementUpdate {
  description?: string
  settlement_date?: string
  version: number
}

export interface Settlement {
  id: number
  group_id: number
  paid_by_user_id: number
  paid_to_user_id: number
  amount: string
  settlement_date: string
  description?: string
  created_by_user_id: number
  created_at: string
  updated_at: string
  version: number
  group?: any // Group type would be imported
  payer?: UserPublic | null
  payee?: UserPublic | null
  created_by_user?: UserPublic | null
}

export type SettlementPublic = Settlement

// Financial Summary types
export interface SummaryUser {
  id: number
  name: string
}

export interface DebtCredit {
  user: SummaryUser
  amount: number
}

export interface UserFinancialSummary {
  net_balance: number
  total_group_spending: number
  debts: DebtCredit[]
  credits: DebtCredit[]
  currency: string
}

export interface FinancialActivity {
  activities: (Expense | Settlement)[]
}

// For creating expense splits
export interface ExpenseSplitCreate {
  user_id: number
  owed_amount?: number
  share_percentage?: number
  share_units?: number
}

export interface ExpenseSplit {
  id: number
  expense_id: number
  user_id: number
  owed_amount: string // String representation of Decimal
  share_percentage?: string | null
  share_units?: number | null
  created_at: string
  updated_at: string
  status: ExpenseSplitStatusEnum
  paid_at?: string | null
  settlement_activities: SettlementActivity[]
  user?: UserPublic | null
}

export interface RecurrencePatternCreate {
  type: 'daily' | 'weekly' | 'monthly' | 'yearly'
  interval: number
  daysOfWeek?: number[]
  endDate?: string
  maxOccurrences?: number
}

export interface RecurrencePattern {
  id: number
  type: 'daily' | 'weekly' | 'monthly' | 'yearly'
  interval: number
  daysOfWeek?: number[]
  endDate?: string
  maxOccurrences?: number
  createdAt: string
  updatedAt: string
}

// For creating expenses
export interface ExpenseCreate {
  description: string
  total_amount: number
  currency?: string
  expense_date?: string
  split_type: string
  list_id?: number
  group_id?: number
  item_id?: number
  paid_by_user_id: number
  is_recurring?: boolean
  recurrence_pattern?: RecurrencePatternCreate
  splits_in?: ExpenseSplitCreate[]
}

export interface ExpenseUpdate {
  description?: string
  currency?: string
  expense_date?: string
  version: number
}

export interface Expense {
  id: number
  description: string
  total_amount: string // String representation of Decimal
  currency: string
  expense_date?: string
  split_type: string
  list_id?: number | null
  group_id?: number | null
  item_id?: number | null
  paid_by_user_id: number
  paid_by_user?: UserPublic | null
  created_by_user_id: number
  created_by_user?: UserPublic | null
  created_at: string
  updated_at: string
  version: number
  splits: ExpenseSplit[]

  overall_settlement_status: ExpenseOverallStatusEnum
  is_recurring: boolean
  isRecurring?: boolean
  next_occurrence?: string
  last_occurrence?: string
  recurrence_pattern?: RecurrencePattern
  parent_expense_id?: number
  generated_expenses?: Expense[]
}

export type SplitType = 'EQUAL' | 'EXACT_AMOUNTS' | 'PERCENTAGE' | 'SHARES' | 'ITEM_BASED';
export type SettlementStatus = 'unpaid' | 'paid' | 'partially_paid';

// Financial audit log types
export interface FinancialAuditLog {
  id: number
  timestamp: string
  user_id?: number
  action_type: string
  entity_type: string
  entity_id: number
  details?: any
  user?: UserPublic
}
