import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from '@nestjs/common';
import { PayrollService } from './payroll.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@Controller('hr/payroll')
@UseGuards(JwtAuthGuard)
export class PayrollController {
  constructor(private service: PayrollService) {}

  @Get()
  list(@Query('month') month?: string, @Query('year') year?: string) {
    return this.service.list(
      month ? parseInt(month, 10) : undefined,
      year ? parseInt(year, 10) : undefined,
    );
  }

  @Get('calculate')
  calculate(
    @Query('employeeId') employeeId: string,
    @Query('month') month: string,
    @Query('year') year: string,
  ) {
    return this.service.calculate(
      parseInt(employeeId, 10),
      parseInt(month, 10),
      parseInt(year, 10),
    );
  }

  @Post('generate')
  generate(@Body() body: { month: number; year: number }) {
    const m = body.month ?? new Date().getMonth() + 1;
    const y = body.year ?? new Date().getFullYear();
    return this.service.generate(m, y);
  }

  @Patch(':id/approve')
  approve(@Param('id') id: string) {
    return this.service.approve(parseInt(id, 10));
  }

  @Patch(':id/paid')
  markPaid(@Param('id') id: string) {
    return this.service.markPaid(parseInt(id, 10));
  }
}
