import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { JobCategoriesService } from './job_categories.service';
import { JobCategories } from './entities/job_categories.entity';
import { Public } from '../auth/public.decorator';
import {
  ApiBearerAuth,
  ApiTags,
  ApiOperation,
  ApiBody,
  ApiParam,
} from '@nestjs/swagger';

@ApiTags('job_categories')
@ApiBearerAuth('access-token')
@Controller('job_categories')
export class JobCategoriesController {
  constructor(private readonly jobCategoriesService: JobCategoriesService) {}
  @Public()
  @Get()
  @ApiOperation({ summary: 'Get all job categories' })
  async findAll(): Promise<JobCategories[]> {
    try {
      return await this.jobCategoriesService.findAll();
    } catch (error) {
      throw new HttpException(
        'Failed to fetch job categories',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get job category by id' })
  @ApiParam({ name: 'id', type: Number })
  async findOne(@Param('id') id: number): Promise<JobCategories> {
    try {
      return await this.jobCategoriesService.findOne(+id);
    } catch (error) {
      throw new HttpException(
        error.message,
        error.status || HttpStatus.NOT_FOUND,
      );
    }
  }

  @Post()
  @ApiOperation({ summary: 'Create job category' })
  @ApiBody({ type: JobCategories })
  async create(@Body() data: Partial<JobCategories>): Promise<JobCategories> {
    try {
      return await this.jobCategoriesService.create(data);
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
    }
  }

  @Put(':id')
  @ApiOperation({ summary: 'Update job category' })
  @ApiParam({ name: 'id', type: Number })
  @ApiBody({ type: JobCategories })
  async update(
    @Param('id') id: number,
    @Body() data: Partial<JobCategories>,
  ): Promise<JobCategories> {
    try {
      return await this.jobCategoriesService.update(+id, data);
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
    }
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete job category' })
  @ApiParam({ name: 'id', type: Number })
  async remove(@Param('id') id: number): Promise<{ message: string }> {
    try {
      return await this.jobCategoriesService.remove(+id);
    } catch (error) {
      throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
    }
  }
}
