import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ParseIntPipe,
  Query,
  UseGuards,
} from "@nestjs/common"
import { ClientTypeService } from "./client-type.service"
import { CreateClientTypeDto } from "../dto/create-client-type.dto"
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiQuery,
  ApiBearerAuth,
} from "@nestjs/swagger"
import { ClientType } from "../entities/client-type.entity"
import { AuthGuardMiddleware } from "src/middleware/auth-guard.middleware"
import { UpdateClientTypeDto } from "../dto/update-clinet-type.dto"

@ApiTags("Client Type")
@UseGuards(AuthGuardMiddleware)
@ApiBearerAuth("access-token")
@Controller("client-type")
export class ClientTypeController {
  constructor(private readonly clientTypeService: ClientTypeService) {}

  @Post()
  @ApiOperation({ summary: "Create a new client type" })
  @ApiResponse({
    status: 201,
    description: "Client type created successfully",
    type: ClientType,
  })
  create(@Body() createDto: CreateClientTypeDto) {
    return this.clientTypeService.create(createDto)
  }

  @Get()
  @ApiOperation({
    summary: "Get all client types with pagination and search",
  })
  @ApiQuery({
    name: "limit",
    required: false,
    description: "Number of records per page",
  })
  @ApiQuery({
    name: "skip",
    required: false,
    description: "Number of records to skip",
    example: 0,
  })
  @ApiQuery({
    name: "search",
    required: false,
    type: String,
    description: "Search by name",
  })
  @ApiQuery({
    name: "sortBy",
    required: false,
    type: String,
    description: "Sort column: id, name, created_at, updated_at",
  })
  @ApiQuery({
    name: "sortOrder",
    required: false,
    type: String,
    enum: ["ASC", "DESC"],
    description: "Sort order",
  })
  findAll(
    @Query("limit", new ParseIntPipe({ optional: true })) limit?: string,
    @Query("skip", new ParseIntPipe({ optional: true })) skip?: string,
    @Query("search") search?: string,
    @Query("sortBy") sortBy?: string,
    @Query("sortOrder") sortOrder?: string,
  ) {
    return this.clientTypeService.findAll(
      limit,
      skip,
      search,
      sortBy,
      sortOrder,
    )
  }

  @Get(":id")
  @ApiOperation({ summary: "Get a client type by id" })
  @ApiResponse({
    status: 200,
    description: "Return the client type",
    type: ClientType,
  })
  findOne(@Param("id", ParseIntPipe) id: number) {
    return this.clientTypeService.findOne(id)
  }

  @Patch(":id")
  @ApiOperation({ summary: "Update a client type" })
  @ApiResponse({
    status: 200,
    description: "Client type updated successfully",
    type: ClientType,
  })
  update(
    @Param("id", ParseIntPipe) id: number,
    @Body() updateDto: UpdateClientTypeDto,
  ) {
    return this.clientTypeService.update(id, updateDto)
  }

  @Delete(":id")
  @ApiOperation({ summary: "Delete a client type" })
  @ApiResponse({
    status: 200,
    description: "Client type deleted successfully",
  })
  remove(@Param("id", ParseIntPipe) id: number) {
    return this.clientTypeService.remove(id)
  }
}
