import {
  Controller,
  Get,
  Post,
  Param,
  UseInterceptors,
  UploadedFile,
  Body,
  Query,
  Patch,
  Req,
  UnauthorizedException,
  UseGuards,
} from '@nestjs/common';
import { TownService } from './town.service';
import { ApiBearerAuth, 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';
import { Roles } from 'src/auth/role.decorator';
import { ALL_ROLES, SUPERADMIN_ROLES } from 'src/shared/enum/admin-role.enum';
import { CreateTownReqDto } from './dto/createTownReq.dto';
import { CustomAdminRequest } from 'src/auth/admin/interface/customAdminReq';
import { AuthAdminGuard } from 'src/auth/admin/authAdmin.guard';
@Controller()
@ApiTags('Pueblos')
export class TownController {
  constructor(private readonly townService: TownService) {}

  @UseGuards(AuthAdminGuard)
  @Roles(SUPERADMIN_ROLES)
  @ApiBearerAuth('jwt')
  @ApiBody({ type: CreateTownReqDto })
  @ApiConsumes('multipart/form-data')
  @Post('town')
  @UseInterceptors(fileInterceptor('image', 'static/towns/', ['.jpg', '.jpeg', '.png']))
  async create(@UploadedFile(new FileValidationPipe()) file, @Body() createTownReqDto: CreateTownReqDto) {
    try {
      const createTownDto: CreateTownDto = {
        name: createTownReqDto.name,
        imageName: file.filename,
        descriptionEN: createTownReqDto.descriptionEN,
        descriptionES: createTownReqDto.descriptionES,
        state: createTownReqDto.state,
      };
      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;
    }
  }

  @UseGuards(AuthAdminGuard)
  @Roles([ALL_ROLES.ADMIN])
  @ApiBearerAuth('jwt')
  @ApiBody({ type: CreateTownReqDto })
  @ApiConsumes('multipart/form-data')
  @Patch('town/:idTown')
  @UseInterceptors(fileInterceptor('image', 'static/towns/', ['.jpg', '.jpeg', '.png']))
  async update(
    @Param('idTown') idTown: number,
    @UploadedFile(new FileValidationPipe()) file,
    @Body() createTownReqDto: CreateTownReqDto,
    @Req() req: CustomAdminRequest,
  ) {
    try {
      if (req.admin.idTown.townId != idTown) {
        throw new UnauthorizedException('This is not your assigned town');
      }
      const updateTownDto: CreateTownDto = {
        name: createTownReqDto.name,
        imageName: file.filename,
        descriptionEN: createTownReqDto.descriptionEN,
        descriptionES: createTownReqDto.descriptionES,
        state: createTownReqDto.state,
      };
      await this.townService.update(idTown, updateTownDto);
      return { message: 'Town updated successfully' };
    } catch (error) {
      throw error;
    }
  }

  @Get('town/:idTown')
  async findOne(@Param('idTown') idTown: number) {
    try {
      return await this.townService.findOne(idTown);
    } catch (error) {
      throw error;
    }
  }
}
