import {
  Controller,
  Get,
  Query,
  Post,
  Body,
  Put,
  Param,
  Patch,
  Delete,
} from '@nestjs/common';
import { Public } from '../auth/public.decorator';
import { AreaService } from './area.service';
import {
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiOperation,
} from '@nestjs/swagger';

@ApiTags('area')
@ApiBearerAuth('access-token')
@Controller('area')
export class AreaController {
  constructor(private readonly areaService: AreaService) {}

  @Public()
  @Get()
  @ApiQuery({ name: 'status', required: false, type: Number })
  async getAreas(@Query('status') status?: any) {
    const s =
      status !== undefined && !isNaN(Number(status))
        ? Number(status)
        : undefined;
    return this.areaService.getAreas(s);
  }

  @Public()
  @Get('pincodes')
  @ApiOperation({ summary: 'Get All Pincodes' })
  async getAllPincodes() {
    return this.areaService.getAllPincodes();
  }

  @Public()
  @Get('by_pincode')
  @ApiQuery({ name: 'pincode', required: true, type: String })
  @ApiQuery({ name: 'status', required: false, type: Number })
  async getAreasByPincode(
    @Query('pincode') pincode: string,
    @Query('status') status?: any,
  ) {
    if (!pincode) {
      return { message: 'Pincode is required', data: [] };
    }
    const s =
      status !== undefined && !isNaN(Number(status))
        ? Number(status)
        : undefined;
    return this.areaService.getAreasByPincode(pincode, s);
  }
  @Public()
  @Get('filtered')
  @ApiQuery({ name: 'status', required: false, type: Number })
  findByCity(
    @Query('city_id') cityId: any,
    @Query('page') page: any = 1,
    @Query('limit') limit: any = 10,
    @Query('status') status?: any,
  ) {
    const cId = !isNaN(Number(cityId)) ? Number(cityId) : undefined;
    const p = !isNaN(Number(page)) ? Number(page) : 1;
    const l = !isNaN(Number(limit)) ? Number(limit) : 10;
    const s =
      status !== undefined && !isNaN(Number(status))
        ? Number(status)
        : undefined;

    if (cId === undefined) {
      return {
        success: false,
        message: 'City ID is required and must be a number',
      };
    }

    return this.areaService.findByCity(cId, p, l, s);
  }

  @Public()
  @Get(':id')
  findOne(@Param('id') id: string) {
    if (isNaN(Number(id))) {
      return { success: false, message: 'Invalid ID' };
    }
    return this.areaService.findOne(+id);
  }

  @Public()
  @Post()
  @ApiOperation({ summary: 'Create Area' })
  @ApiBody({ schema: { type: 'object', description: 'Area data' } })
  create(@Body() body: any) {
    return this.areaService.create(body);
  }

  @Public()
  @Put(':id')
  @ApiOperation({ summary: 'Update Area' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({ schema: { type: 'object', description: 'Area data' } })
  update(@Param('id') id: string, @Body() body: any) {
    return this.areaService.update(+id, body);
  }

  @Public()
  @Patch(':id')
  @ApiOperation({ summary: 'Patch Area' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({ schema: { type: 'object', description: 'Area data' } })
  patch(@Param('id') id: string, @Body() body: any) {
    return this.areaService.update(+id, body);
  }

  @Public()
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.areaService.remove(+id);
  }
}
