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

@ApiTags('city')
@ApiBearerAuth('access-token')
@Controller('city')
export class CityController {
  constructor(private readonly cityService: CityService) {}
  @Public()
  @Get()
  @ApiOperation({ summary: 'Get All Cities' })
  findAll() {
    return this.cityService.findAll();
  }

  @Public()
  @Get('search')
  @ApiOperation({ summary: 'Search Cities by Name' })
  @ApiQuery({ name: 'q', required: true, type: String })
  async search(@Query('q') query: string) {
    return this.cityService.search(query);
  }

  @Public()
  @Get('featured')
  @ApiOperation({ summary: 'Get Featured Cities' })
  getFeatured() {
    return this.cityService.findFeatured();
  }

  @Public()
  @Get('filtered')
  @ApiOperation({ summary: 'Filter Cities by State' })
  @ApiQuery({ name: 'status', required: false, type: Number })
  findByState(
    @Query('state_id') stateId: any,
    @Query('page') page: any = 1,
    @Query('limit') limit: any = 10,
    @Query('status') status?: any,
  ) {
    const sId = !isNaN(Number(stateId)) ? Number(stateId) : 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 (sId === undefined) {
      return {
        success: false,
        message: 'State ID is required and must be a number',
      };
    }

    return this.cityService.findByState(sId, p, l, s);
  }

  @Public()
  @Get('front-order')
  @ApiOperation({ summary: 'Get Cities by Front Order' })
  getCitiesByFrontOrder() {
    return this.cityService.getCitiesByFrontOrder();
  }

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

  @Public()
  @Post()
  @ApiOperation({ summary: 'Create City' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        city_name: { type: 'string' },
        state_id: { type: 'number' },
        status: { type: 'number', example: 1 },
      },
      required: ['city_name', 'state_id'],
    },
  })
  create(@Body() body: any) {
    return this.cityService.create(body);
  }

  @Public()
  @Put(':id')
  @ApiOperation({ summary: 'Update City' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        city_name: { type: 'string' },
        state_id: { type: 'number' },
        status: { type: 'number', example: 1 },
      },
    },
  })
  update(@Param('id') id: string, @Body() body: any) {
    return this.cityService.update(+id, body);
  }

  @Public()
  @Patch(':id')
  @ApiOperation({ summary: 'Patch City' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        city_name: { type: 'string' },
        state_id: { type: 'number' },
        status: { type: 'number', example: 1 },
      },
    },
  })
  patch(@Param('id') id: string, @Body() body: any) {
    return this.cityService.update(+id, body);
  }

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