import { ApiClient } from './base'
import type { Currency } from './data'
import { type Page, type PageRequest } from './pagination'
import type { Profile } from './profile'

export interface TransactionVendor {
  id: number
  name: string
  description: string
}

export interface TransactionVendorPayload {
  name: string
  description: string
}

export interface TransactionCategory {
  id: number
  parentId: number | null
  name: string
  description: string
  color: string
}

export interface Transaction {
  id: number
  timestamp: string
  addedAt: string
  amount: number
  currency: string
  description: string
  vendorId: number | null
  categoryId: number | null
}

export interface TransactionsListItem {
  id: number
  timestamp: string
  addedAt: string
  amount: number
  currency: Currency
  description: string
  vendor: TransactionsListItemVendor | null
  category: TransactionsListItemCategory | null
  creditedAccount: TransactionsListItemAccount | null
  debitedAccount: TransactionsListItemAccount | null
}

export interface TransactionsListItemVendor {
  id: number
  name: string
}

export interface TransactionsListItemCategory {
  id: number
  name: string
  color: string
}

export interface TransactionsListItemAccount {
  id: number
  name: string
  type: string
  numberSuffix: string
}

export interface TransactionDetail {
  id: number
  timestamp: string
  addedAt: string
  amount: number
  currency: Currency
  description: string
  vendor: TransactionVendor | null
  category: TransactionCategory | null
  creditedAccount: TransactionDetailAccount | null
  debitedAccount: TransactionDetailAccount | null
  tags: string[]
  lineItems: TransactionDetailLineItem[]
}

export interface TransactionDetailAccount {
  id: number
  name: string
  type: string
  numberSuffix: string
}

export interface TransactionDetailLineItem {
  idx: number
  valuePerItem: number
  quantity: number
  description: number
  category: TransactionCategory | null
}

export class TransactionApiClient extends ApiClient {
  readonly path: string

  constructor(profile: Profile) {
    super()
    this.path = `/profiles/${profile.name}`
  }

  async getVendors(): Promise<TransactionVendor[]> {
    return await super.getJson(this.path + '/vendors')
  }

  async getVendor(id: number): Promise<TransactionVendor> {
    return await super.getJson(this.path + '/vendors/' + id)
  }

  async createVendor(data: TransactionVendorPayload): Promise<TransactionVendor> {
    return await super.postJson(this.path + '/vendors', data)
  }

  async updateVendor(id: number, data: TransactionVendorPayload): Promise<TransactionVendor> {
    return await super.putJson(this.path + '/vendors/' + id, data)
  }

  async deleteVendor(id: number): Promise<void> {
    return await super.delete(this.path + '/vendors/' + id)
  }

  async getTransactions(
    paginationOptions: PageRequest | undefined = undefined,
  ): Promise<Page<TransactionsListItem>> {
    return await super.getJsonPage(this.path + '/transactions', paginationOptions)
  }

  async getTransaction(id: number): Promise<TransactionDetail> {
    return await super.getJson(this.path + '/transactions/' + id)
  }
}
