import { Controller, Get, Post, Patch, Param, Body, Query } from '@nestjs/common'
import { TimeEntriesService } from './time-entries.service'
import { CreateTimeEntryDto } from './dto/create-time-entry.dto'
import { UpdateTimeEntryDto } from './dto/update-time-entry.dto'

@Controller('time-entries')
export class TimeEntriesController {
  constructor(private readonly service: TimeEntriesService) {}

  @Post()
  create(@Body() dto: CreateTimeEntryDto) {
    return this.service.create(dto)
  }

  @Get()
  findAll(
    @Query('matterId') matterId?: string,
    @Query('clientFolderId') clientFolderId?: string,
    @Query('attorney') attorney?: string,
    @Query('billed') billed?: string,
    @Query('from') from?: string,
    @Query('to') to?: string,
    @Query('limit') limit?: string,
    @Query('offset') offset?: string,
  ) {
    return this.service.findAll({
      matterId, clientFolderId, attorney,
      billed: billed !== undefined ? billed === 'true' : undefined,
      from, to,
      limit: limit ? parseInt(limit) : undefined,
      offset: offset ? parseInt(offset) : undefined,
    })
  }

  @Get('summary/:matterId')
  summary(@Param('matterId') matterId: string) {
    return this.service.getSummary(matterId)
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.service.findOne(id)
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() dto: UpdateTimeEntryDto) {
    return this.service.update(id, dto)
  }

  @Post('mark-billed')
  markBilled(@Body('ids') ids: string[], @Body('invoiceRef') invoiceRef: string) {
    return this.service.markBilled(ids, invoiceRef)
  }
}
