import {
  Controller,
  Get,
  Post,
  Patch,
  Body,
  Param,
  Delete,
} from '@nestjs/common';
import { CohortService } from './cohort.service';
import { CreateCohortDto } from './dto/create-cohort.dto';
import { UpdateCohortDto } from './dto/update-cohort.dto';
import { HttpException } from '@nestjs/common/exceptions';
import { HttpStatus } from '@nestjs/common/enums';
import addCommonDbFields from '../../utility/commonField';
import { cohortExist } from '../../config/message';
import {
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiOperation,
} from '@nestjs/swagger';
import { Public } from '../auth/public.decorator';

@ApiTags('cohort')
@ApiBearerAuth('access-token')
@Controller('cohort')
export class CohortController {
  constructor(private readonly cohortService: CohortService) {}

  @Public()
  @Post()
  @ApiOperation({ summary: 'Create Cohort' })
  @ApiBody({ type: CreateCohortDto })
  async create(@Body() createCohortDto: CreateCohortDto) {
    try {
      const cohortCheck = await this.cohortService.findByCohortId(
        createCohortDto.cohortId,
      );

      const cohortName = await this.cohortService.findByCohortName(
        createCohortDto.cohortName,
        createCohortDto.cohortGroupId,
      );

      if (cohortName) {
        throw new HttpException(
          cohortExist(createCohortDto.cohortName),
          HttpStatus.BAD_REQUEST,
        );
      }

      //If Cohort Present then update else insert
      if (cohortCheck) {
        return this.cohortService.update(
          +cohortCheck.cohortPk,
          createCohortDto,
        );
      } else {
        //Add the Audit fields
        createCohortDto = addCommonDbFields(createCohortDto);
        return this.cohortService.create(createCohortDto);
      }
    } catch (exception) {
      throw new HttpException(exception.message, HttpStatus.BAD_REQUEST);
    }
  }

  @Public()
  @Get()
  @ApiOperation({ summary: 'Get All Cohorts' })
  findAll() {
    return this.cohortService.findAll();
  }

  @Public()
  @Get(':id')
  @ApiOperation({ summary: 'Get Cohort by ID' })
  @ApiParam({ name: 'id', type: String })
  findOne(@Param('id') id: string) {
    return this.cohortService.findOne(+id);
  }

  @Public()
  @Patch(':id')
  @ApiOperation({ summary: 'Update Cohort' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({ type: UpdateCohortDto })
  async update(
    @Param('id') id: string,
    @Body() updateCohortDto: UpdateCohortDto,
  ) {
    try {
      const cohortName = await this.cohortService.findByCohortName(
        updateCohortDto.cohortName,
        updateCohortDto.cohortGroupId,
      );

      if (cohortName) {
        throw new HttpException(
          cohortExist(updateCohortDto.cohortName),
          HttpStatus.BAD_REQUEST,
        );
      }
      return await this.cohortService.update(+id, updateCohortDto);
    } catch (exception) {
      throw new HttpException(exception.message, HttpStatus.BAD_REQUEST);
    }
  }

  @Public()
  @Delete(':id')
  @ApiOperation({ summary: 'Delete Cohort' })
  @ApiParam({ name: 'id', type: String })
  async remove(@Param('id') id: string) {
    return await this.cohortService.remove(+id);
  }
  @Public()
  @Get(':id/employee')
  @ApiOperation({ summary: 'Find Employee Cohort' })
  @ApiParam({ name: 'id', type: String })
  async findEmployeeCohort(@Param('id') id: string) {
    return await this.cohortService.findEmployeeCohort(+id);
  }
}
