// fe/src/types/unified-expense.ts
// Unified Expense Types - Canonical data contract

import type { BaseEntity } from './shared'
import type { UserPublic } from './user'

/**
 * Enums - Use exact backend enum values
 */
export enum SplitType {
    EQUAL = 'EQUAL',
    EXACT_AMOUNTS = 'EXACT_AMOUNTS',
    PERCENTAGE = 'PERCENTAGE',
    SHARES = 'SHARES',
    ITEM_BASED = 'ITEM_BASED'
}

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

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

export enum RecurrenceType {
    DAILY = 'DAILY',
    WEEKLY = 'WEEKLY',
    MONTHLY = 'MONTHLY',
    YEARLY = 'YEARLY'
}

/**
 * Recurrence Pattern
 */
export interface RecurrencePattern extends BaseEntity {
    type: RecurrenceType
    interval: number
    days_of_week?: string | null // JSON string of array
    end_date?: string | null
    max_occurrences?: number | null
}

export interface RecurrencePatternCreate {
    type: RecurrenceType
    interval: number
    days_of_week?: number[]
    end_date?: string
    max_occurrences?: number
}

/**
 * Settlement Activity - tracks individual payments against splits
 */
export interface SettlementActivity extends BaseEntity {
    expense_split_id: number
    paid_by_user_id: number
    paid_at: string
    amount_paid: string // Decimal as string
    created_by_user_id: number
    payer?: UserPublic | null
    creator?: UserPublic | null
}

export interface SettlementActivityCreate {
    expense_split_id: number
    paid_by_user_id: number
    amount_paid: string
    paid_at?: string
}

/**
 * Expense Split
 */
export interface ExpenseSplit extends BaseEntity {
    expense_id: number
    user_id: number
    owed_amount: string // Decimal as string
    share_percentage?: string | null // Decimal as string
    share_units?: number | null
    status: ExpenseSplitStatus
    paid_at?: string | null
    settlement_activities: SettlementActivity[]
    user?: UserPublic | null
}

export interface ExpenseSplitCreate {
    user_id: number
    owed_amount?: string // Decimal as string for precision
    share_percentage?: string
    share_units?: number
}

/**
 * Main Expense Entity
 */
export interface Expense extends BaseEntity {
    description: string
    total_amount: string // Decimal as string
    currency: string
    expense_date: string
    split_type: SplitType
    list_id?: number | null
    group_id?: number | null
    item_id?: number | null
    paid_by_user_id: number
    created_by_user_id: number
    overall_settlement_status: ExpenseOverallStatus
    is_recurring: boolean
    next_occurrence?: string | null
    last_occurrence?: string | null
    parent_expense_id?: number | null

    // Relationships
    paid_by_user?: UserPublic | null
    created_by_user?: UserPublic | null
    splits: ExpenseSplit[]
    recurrence_pattern?: RecurrencePattern | null
    generated_expenses?: Expense[]
}

export interface ExpenseCreate {
    description: string
    total_amount: string // Use string for decimal precision
    currency?: string
    expense_date?: string
    split_type: SplitType
    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
    total_amount?: string
    currency?: string
    expense_date?: string
    split_type?: SplitType
    list_id?: number
    group_id?: number
    item_id?: number
    version: number
    is_recurring?: boolean
    recurrence_pattern?: RecurrencePatternCreate
    next_occurrence?: string
}

/**
 * Settlement between users
 */
export interface Settlement extends BaseEntity {
    group_id: number
    paid_by_user_id: number
    paid_to_user_id: number
    amount: string // Decimal as string
    settlement_date: string
    description?: string | null
    created_by_user_id: number
    payer?: UserPublic | null
    payee?: UserPublic | null
    created_by_user?: UserPublic | null
}

export interface SettlementCreate {
    group_id: number
    paid_by_user_id: number
    paid_to_user_id: number
    amount: string
    settlement_date?: string
    description?: string
}

/**
 * Real-time events for expenses
 */
export interface ExpenseRealtimeEvents {
    'expense:created': { expense: Expense }
    'expense:updated': { expense: Expense }
    'expense:deleted': { expense_id: number }
    'expense:split_settled': { expense_id: number; split_id: number; settlement: SettlementActivity }
    'expense:editing_started': { expense_id: number; user_id: number; user_name: string; field?: string }
    'expense:editing_stopped': { expense_id: number }
    'settlement:created': { settlement: Settlement }
    'settlement:updated': { settlement: Settlement }
} 