import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards, Patch, Param, Delete } from '@nestjs/common';
import { AdminService } from '../providers/admin.service';
import { CreateAdminDto } from '../dto/create-admin.dto';
import { UpdateAdminDto } from '../dto/update-admin.dto';
import { ApiTags, ApiOperation, ApiBody, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
import { RestId } from 'src/common/decorators';
import { AdminId } from 'src/common/decorators/admin-id.decorator';
import { Admin } from '../entities/admin.entity';
import { Permission } from 'src/common/enums/permission.enum';
import { Permissions } from 'src/common/decorators/permissions.decorator';
import { CreateMyRestaurantAdminDto } from '../dto/create-my-restaurant-admin.dto';

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

  @Get('admin/admins')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ADMINS)
  getAll(@RestId() restId: string) {
    return this.adminService.findAllByRestaurantId(restId);
  }

  @Get('admin/admins/profile')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ADMINS)
  getMe(@AdminId() adminId: string, @RestId() restId: string) {
    return this.adminService.finAdminById(adminId, restId);
  }

  @Post('admin/admins')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ADMINS)
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: 'Create a new admin' })
  @ApiBody({ type: CreateAdminDto })
  async create(@Body() dto: CreateAdminDto, @RestId() restId: string) {
    const admin = await this.adminService.createAdminForMyRestaurant(restId, dto);
    return admin;
  }

  @Patch('admin/admins/:adminId')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ADMINS)
  @ApiOperation({ summary: 'Update an admin' })
  @ApiParam({ name: 'adminId', description: 'Admin ID' })
  update(@Param('adminId') adminId: string, @Body() dto: UpdateAdminDto,
    @RestId() restId: string) {
    return this.adminService.update(adminId, restId, dto);
  }

  @Get('admin/admins/:adminId')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ADMINS)
  getById(@Param('adminId') adminId: string, @RestId() restId: string) {
    return this.adminService.finAdminById(adminId, restId);
  }

  @Delete('admin/admins/:adminId')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ADMINS)
  @ApiOperation({ summary: 'Delete an admin by ID' })
  @ApiParam({ name: 'id', description: 'Admin ID' })
  deleteById(@Param('adminId') adminId: string, @RestId() restId: string) {
    return this.adminService.remove(adminId, restId);
  }

  /** Super Admin Endpoints */
  @UseGuards(SuperAdminAuthGuard)
  @Get('super-admin/restaurants/:restaurantId/admins')
  @ApiOperation({ summary: 'Get admins for a specific restaurant (Super Admin only)' })
  @ApiParam({ name: 'restaurantId', description: 'Restaurant ID' })
  getAdminForRestaurant(@Param('restaurantId') restaurantId: string) {
    return this.adminService.findAllByRestaurantId(restaurantId);
  }

  @UseGuards(SuperAdminAuthGuard)
  @Post('super-admin/restaurants/:restaurantId/admins')
  @ApiOperation({ summary: 'Create admin for a specific restaurant (Super Admin only)' })
  @ApiParam({ name: 'restaurantId', description: 'Restaurant ID' })
  @ApiBody({ type: CreateMyRestaurantAdminDto })
  createAdminForRestaurant(
    @Param('restaurantId') restaurantId: string,
    @Body() dto: CreateMyRestaurantAdminDto,
  ): Promise<Admin> {
    return this.adminService.createAdminForRestaurantBySuperAdmin(restaurantId, dto);
  }

  @UseGuards(SuperAdminAuthGuard)
  @Delete('super-admin/restaurants/:restaurantId/admins/:adminId')
  @ApiOperation({ summary: 'Revoke admin role from a restaurant (Super Admin only)' })
  @ApiParam({ name: 'restaurantId', description: 'Restaurant ID' })
  @ApiParam({ name: 'adminId', description: 'Admin ID' })
  revokeAdminFromRestaurant(
    @Param('restaurantId') restaurantId: string,
    @Param('adminId') adminId: string,
  ) {
    return this.adminService.remove(adminId, restaurantId);
  }
}
