import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
import { AttendanceService } from './attendance.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@Controller('hr/attendance')
@UseGuards(JwtAuthGuard)
export class AttendanceController {
  constructor(private service: AttendanceService) {}

  @Post()
  createManual(
    @Body() body: { employeeId: number; punchTime: string; punchType?: string },
  ) {
    return this.service.createManual({
      employeeId: body.employeeId,
      punchTime: new Date(body.punchTime),
      punchType: body.punchType || 'in',
    });
  }

  @Get()
  findByEmployee(
    @Query('employeeId') employeeId: string,
    @Query('from') from?: string,
    @Query('to') to?: string,
  ) {
    if (!employeeId) return [];
    return this.service.findByEmployee(
      parseInt(employeeId, 10),
      from ? new Date(from) : undefined,
      to ? new Date(to) : undefined,
    );
  }

  @Get('range')
  findByDateRange(
    @Query('from') from: string,
    @Query('to') to: string,
    @Query('employeeId') employeeId?: string,
  ) {
    if (!from || !to) return [];
    return this.service.findByDateRange(
      new Date(from),
      new Date(to),
      employeeId ? parseInt(employeeId, 10) : undefined,
    );
  }
}
