import { Controller, Get, Post, Param, Body, Query } from '@nestjs/common'
import { TrustService } from './trust.service'
import { CreateTrustTransactionDto, ReconcileDto } from './dto/create-trust-transaction.dto'

@Controller('trust')
export class TrustController {
  constructor(private readonly service: TrustService) {}

  // Firm-level: all trust accounts
  @Get('accounts')
  getAllAccounts(
    @Query('limit')  limit?:  string,
    @Query('offset') offset?: string,
  ) {
    return this.service.getAllAccounts(
      limit  ? parseInt(limit)  : undefined,
      offset ? parseInt(offset) : undefined,
    )
  }

  // Client-level: get trust account (creates if not exists)
  @Get('accounts/:clientFolderId')
  getAccount(@Param('clientFolderId') clientFolderId: string) {
    return this.service.getOrCreate(clientFolderId)
  }

  // Client-level: transaction history
  @Get('transactions')
  getTransactions(
    @Query('clientFolderId') clientFolderId: string,
    @Query('limit')          limit?:          string,
    @Query('offset')         offset?:         string,
  ) {
    return this.service.getTransactions(
      clientFolderId,
      limit  ? parseInt(limit)  : undefined,
      offset ? parseInt(offset) : undefined,
    )
  }

  // Post a new trust transaction
  @Post('transactions')
  addTransaction(@Body() dto: CreateTrustTransactionDto) {
    return this.service.addTransaction(dto)
  }

  // Reconcile: set bank balance and compute variance
  @Post('accounts/:clientFolderId/reconcile')
  reconcile(
    @Param('clientFolderId') clientFolderId: string,
    @Body() dto: ReconcileDto,
  ) {
    return this.service.reconcile(clientFolderId, dto)
  }
}
