import { Controller, Get, Param, Req, UseGuards } from '@nestjs/common';
import { AdminService } from './admin.service';
import { ApiBearerAuth, ApiParam, ApiTags } from '@nestjs/swagger';
import { ADMIN_ROLES, SUPERADMIN_ROLES } from 'src/shared/enum/admin-role.enum';
import { Roles } from 'src/auth/role.decorator';
import { CustomAdminRequest } from 'src/auth/admin/interface/customAdminReq';
import { AuthAdminGuard } from 'src/auth/admin/authAdmin.guard';

@Controller('')
@ApiTags('Admin')
export class AdminController {
  constructor(private readonly adminService: AdminService) {}

  @UseGuards(AuthAdminGuard)
  @Roles(ADMIN_ROLES)
  @ApiBearerAuth('jwt')
  @Get('admin/whoami')
  async whoAmI(@Req() req: CustomAdminRequest) {
    return {
      email: req.admin.email,
      name: req.admin.name,
      lastName: req.admin.lastName,
      role: req.admin.role,
      idTown: req.admin?.idTown?.townId || null,
    };
  }

  @UseGuards(AuthAdminGuard)
  @Roles(SUPERADMIN_ROLES)
  @ApiParam({ name: 'idTown', type: Number })
  @Get('admin/:idTown')
  @ApiBearerAuth('jwt')
  async findAllByTown(@Param('idTown') idTown: number) {
    try {
      const admins = await this.adminService.findAllByTown(idTown);
      return admins;
    } catch (e) {
      return e;
    }
  }
}
