import { Controller, Get, Post, Body, Param, Patch, Delete, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBody, ApiBearerAuth, ApiHeader } from '@nestjs/swagger';
import { RolesService } from '../providers/roles.service';
import { PermissionsService } from '../providers/permissions.service';
import { CreateRoleDto } from '../dto/create-role.dto';
import { UpdateRoleDto } from '../dto/update-role.dto';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
import { Permissions } from 'src/common/decorators/permissions.decorator';
import { RestId } from 'src/common/decorators';
import { AdminId } from 'src/common/decorators/admin-id.decorator';
import { Permission } from 'src/common/enums/permission.enum';



@ApiTags('roles')
@Controller()
export class RolesController {
  constructor(
    private readonly roleService: RolesService,
    private readonly permissionService: PermissionsService,
  ) { }

  @Get('admin/roles')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ROLES)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get all through restaurant roles with pagination and filters' })
  findAll(@RestId() restId: string) {
    return this.roleService.findAllGeneralAndRestaurantRoles(restId);
  }

  @Get('admin/roles/permissions')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ROLES)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get all permissions that the admin has' })
  async findAllPermissions(@AdminId() adminId: string, @RestId() restId: string) {
    const adminPermissionNames = await this.permissionService.getAdminFullPermissions(adminId, restId);

    return adminPermissionNames;
  }



  @Post('admin/roles')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ROLES)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Create a new role' })
  @ApiBody({ type: CreateRoleDto })
  create(@Body() dto: CreateRoleDto, @RestId() restId: string) {
    return this.roleService.createRestaurantRole(dto, restId);
  }

  @Get('admin/roles/:id')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ROLES)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get a specific role by ID' })
  findOne(@Param('id') id: string, @RestId() restId: string) {
    return this.roleService.findOne(restId, id);
  }

  @Patch('admin/roles/:id')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ROLES)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Update a role' })
  @ApiBody({ type: UpdateRoleDto })
  update(@Param('id') id: string, @Body() dto: UpdateRoleDto, @RestId() restId: string) {
    return this.roleService.update(restId, id, dto);
  }

  @Delete('admin/roles/:id')
  @UseGuards(AdminAuthGuard)
  @Permissions(Permission.MANAGE_ROLES)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Delete a role' })
  remove(@Param('id') id: string, @RestId() restId: string) {
    return this.roleService.remove(restId, id);
  }

  /** Super Admin Endpoints */
  @UseGuards(SuperAdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get all system roles for super-admin' })
  @Get('super-admin/system-roles')
  @ApiOperation({ summary: 'Get all system roles for super-admin' })
  findAllSystemRoles() {
    return this.roleService.findAllSystemRoles();
  }
}
