import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import { EmployeesService } from './employees.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@Controller('hr/employees')
@UseGuards(JwtAuthGuard)
export class EmployeesController {
  constructor(private service: EmployeesService) {}

  @Get()
  findAll(
    @Query('departmentId') departmentId?: string,
    @Query('shiftId') shiftId?: string,
    @Query('status') status?: string,
  ) {
    return this.service.findAll({
      departmentId: departmentId ? parseInt(departmentId, 10) : undefined,
      shiftId: shiftId ? parseInt(shiftId, 10) : undefined,
      status,
    });
  }

  @Get(':id')
  findById(@Param('id') id: string) {
    return this.service.findById(parseInt(id, 10));
  }

  @Post()
  create(
    @Body()
    body: {
      empNo: string;
      firstName: string;
      lastName: string;
      email?: string;
      phone?: string;
      nationalId?: string;
      hireDate?: string;
      birthDate?: string;
      address?: string;
      salary?: number;
      departmentId?: number;
      shiftId?: number;
    },
  ) {
    return this.service.create({
      ...body,
      hireDate: body.hireDate ? new Date(body.hireDate) : undefined,
      birthDate: body.birthDate ? new Date(body.birthDate) : undefined,
    });
  }

  @Patch(':id')
  update(
    @Param('id') id: string,
    @Body()
    body: {
      empNo?: string;
      firstName?: string;
      lastName?: string;
      email?: string;
      phone?: string;
      nationalId?: string;
      hireDate?: string;
      birthDate?: string;
      address?: string;
      salary?: number;
      departmentId?: number;
      shiftId?: number;
      status?: string;
    },
  ) {
    return this.service.update(parseInt(id, 10), {
      ...body,
      hireDate: body.hireDate ? new Date(body.hireDate) : undefined,
      birthDate: body.birthDate ? new Date(body.birthDate) : undefined,
    });
  }

  @Delete(':id')
  delete(@Param('id') id: string) {
    return this.service.delete(parseInt(id, 10));
  }
}
