import { api } from './api'
import type {
    Settlement,
    SettlementCreate,
    SettlementUpdate,
    SettlementActivity,
    SettlementActivityCreate,
    UserFinancialSummary,
    FinancialActivity,
} from '@/types/expense'

export const settlementService = {
    // Generic Settlement operations
    async createSettlement(settlement: SettlementCreate): Promise<Settlement> {
        const response = await api.post('/financials/settlements', settlement)
        return response.data
    },

    async getSettlement(id: number): Promise<Settlement> {
        const response = await api.get(`/financials/settlements/${id}`)
        return response.data
    },

    async updateSettlement(id: number, updates: SettlementUpdate): Promise<Settlement> {
        const response = await api.put(`/financials/settlements/${id}`, updates)
        return response.data
    },

    async deleteSettlement(id: number, expectedVersion?: number): Promise<void> {
        const params = expectedVersion ? { expected_version: expectedVersion } : undefined
        await api.delete(`/financials/settlements/${id}`, { params })
    },

    async getGroupSettlements(groupId: number, params?: { skip?: number; limit?: number }): Promise<Settlement[]> {
        const response = await api.get(`/financials/groups/${groupId}/settlements`, { params })
        return response.data
    },

    // Settlement Activity operations (for expense splits)
    async recordSettlementActivity(
        expenseSplitId: number,
        activity: SettlementActivityCreate
    ): Promise<SettlementActivity> {
        const response = await api.post(`/financials/expense_splits/${expenseSplitId}/settle`, activity)
        return response.data
    },

    async getSettlementActivitiesForSplit(
        expenseSplitId: number,
        params?: { skip?: number; limit?: number }
    ): Promise<SettlementActivity[]> {
        const response = await api.get(`/financials/expense_splits/${expenseSplitId}/settlement_activities`, { params })
        return response.data
    },

    // Financial Summary operations
    async getUserFinancialActivity(): Promise<FinancialActivity> {
        const response = await api.get('/financials/users/me/financial-activity')
        return response.data
    },

    async getUserFinancialSummary(): Promise<UserFinancialSummary> {
        const response = await api.get('/financials/summary/user')
        return response.data
    },

    async getGroupFinancialSummary(groupId: number): Promise<UserFinancialSummary> {
        const response = await api.get(`/financials/summary/group/${groupId}`)
        return response.data
    },
} 