import { Controller, Get, Post, Param, Delete, UseInterceptors, UploadedFile, Body, Query } from '@nestjs/common';
import { TownService } from './town.service';
import { ApiBody, ApiConsumes, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
import { FileValidationPipe } from 'src/shared/pipe/file-validation.pipe';
import { fileInterceptor } from 'src/shared/interceptors/file-save.interceptor';
import { CreateTownDto } from './dto/create-town.dto';
@Controller()
@ApiTags('Pueblos')
export class TownController {
  constructor(
    private readonly townService: TownService,
    // private readonly stateService: StateService,
  ) {}

  @ApiBody({ type: CreateTownDto })
  @ApiConsumes('multipart/form-data')
  @Post('town')
  @UseInterceptors(fileInterceptor('image', 'static/towns/', ['.jpg', '.jpeg', '.png']))
  async create(@UploadedFile(new FileValidationPipe()) file, @Body() createTownDto: CreateTownDto) {
    try {
      createTownDto.imageName = file.filename;
      await this.townService.create(createTownDto);
      return { message: 'Town created successfully' };
    } catch (error) {
      throw error;
    }
  }
  @ApiParam({ name: 'stateId', type: Number })
  @ApiQuery({ name: 'lang', type: String })
  @Get('state/:stateId/town')
  async findTownsByState(@Param('stateId') stateId: number, @Query('lang') lang) {
    try {
      stateId = parseInt(stateId.toString());
      return await this.townService.findTownsByState(stateId, lang);
    } catch (error) {
      throw error;
    }
  }

  @Get()
  findAll() {
    return this.townService.findAll();
  }

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