import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  ValidationPipe,
  ParseIntPipe,
  UseGuards,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiQuery,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { MenuActionsService } from './menu-actions.service';
import { CreateMenuActionsDto } from './dto/create.dto';
import { SearchMenuActionsDto } from './dto/search-dto';
import { UpdateMenuActionsDto } from './dto/update.dto';
import { Response } from 'src/config/pagination/paginationDto';
import { MenuActionsEntity } from 'src/entities/MenuActions.entity';

/**
 * Menu Actions Controller
 *
 * This controller manages CRUD operations for menu actions within the application.
 * Menu actions represent individual operations or permissions that can be performed
 * within menu items (e.g., "create", "read", "update", "delete", "export").
 *
 * Menu actions are typically used for:
 * - Defining granular permissions for role-based access control
 * - Associating specific operations with menu items
 * - Controlling UI element visibility based on user permissions
 *
 * @ApiTags('Menu Actions') - Groups endpoints under 'Menu Actions' in Swagger
 * @Controller('') - No path prefix, consider adding 'menu-actions'
 *
 * @note Missing authentication guards - menu actions are sensitive and should
 * only be accessible to administrators.
 */
@ApiTags('Menu Actions')
@Controller('') // Consider adding path prefix: @Controller('menu-actions')
export class MenuActionsController {
  constructor(private readonly menuActionsService: MenuActionsService) {}

  /**
   * Search and retrieve menu actions with filtering.
   *
   * This endpoint allows searching through menu actions with various filters.
   * Uses query parameters instead of request body for GET requests.
   *
   * @param searchDto - Search criteria and filter parameters
   * @returns Paginated response of menu actions
   *
   * @note Missing ValidationPipe on searchDto query parameter
   * @note Consider adding authentication/authorization
   */
  @Get()
  @ApiOperation({
    summary: 'Search menu actions',
    description: 'Search and filter menu actions with pagination support',
  })
  @ApiResponse({
    status: 200,
    description: 'Menu actions retrieved successfully',
  })
  @ApiResponse({ status: 400, description: 'Invalid search parameters' })
  findAll(
    @Query() searchDto: SearchMenuActionsDto, // Missing ValidationPipe for query params
  ): Promise<Response<MenuActionsEntity>> {
    return this.menuActionsService.findAll(searchDto);
  }

  /**
   * Get a specific menu action by ID.
   *
   * @param id - Numeric ID of the menu action
   * @returns Single menu action entity
   *
   * @note Consider adding @ParseIntPipe for parameter validation
   */
  @Get(':id')
  @ApiOperation({
    summary: 'Get menu action by ID',
    description: 'Retrieve a specific menu action by its ID',
  })
  @ApiParam({ name: 'id', type: Number, description: 'Menu action ID' })
  @ApiResponse({
    status: 200,
    description: 'Menu action retrieved successfully',
  })
  @ApiResponse({ status: 404, description: 'Menu action not found' })
  findOne(@Param('id') id: number) {
    return this.menuActionsService.findOne(id);
  }

  /**
   * Create a new menu action.
   *
   * This endpoint creates a new menu action entry.
   *
   * @param body - Menu action data
   * @returns Newly created menu action entity
   *
   * @bug Hardcoded user ID 2 - should come from authenticated user session
   * @note Missing authentication - should require admin privileges
   */
  @Post()
  @ApiOperation({
    summary: 'Create menu action',
    description: 'Create a new menu action entry',
  })
  @ApiResponse({ status: 201, description: 'Menu action created successfully' })
  @ApiResponse({ status: 400, description: 'Invalid menu action data' })
  @ApiResponse({ status: 409, description: 'Menu action already exists' })
  create(
    @Body(new ValidationPipe()) body: CreateMenuActionsDto,
  ): Promise<MenuActionsEntity> {
    // BUG: Hardcoded user ID 2 - should be from authenticated user
    return this.menuActionsService.create(2, body);
  }

  /**
   * Update an existing menu action.
   *
   * @param id - Numeric ID of the menu action to update
   * @param body - Updated menu action data
   * @returns Update result
   *
   * @note Consider adding @ParseIntPipe for parameter validation
   * @note Missing authentication - should require admin privileges
   */
  @Put(':id')
  @ApiOperation({
    summary: 'Update menu action',
    description: 'Update an existing menu action',
  })
  @ApiParam({
    name: 'id',
    type: Number,
    description: 'Menu action ID to update',
  })
  @ApiResponse({ status: 200, description: 'Menu action updated successfully' })
  @ApiResponse({ status: 404, description: 'Menu action not found' })
  @ApiResponse({ status: 400, description: 'Invalid update data' })
  update(
    @Param('id') id: number, // Missing ParseIntPipe
    @Body(new ValidationPipe()) body: UpdateMenuActionsDto,
  ) {
    return this.menuActionsService.update(id, body);
  }

  /**
   * Delete a menu action.
   *
   * @param id - Numeric ID of the menu action to delete
   * @returns Delete result
   *
   * @note Consider adding @ParseIntPipe for parameter validation
   * @note Missing authentication - should require admin privileges
   * @note Consider soft delete instead of hard delete for audit trail
   */
  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT) // 204 No Content on successful delete
  @ApiOperation({
    summary: 'Delete menu action',
    description: 'Delete a menu action by ID',
  })
  @ApiParam({
    name: 'id',
    type: Number,
    description: 'Menu action ID to delete',
  })
  @ApiResponse({ status: 204, description: 'Menu action deleted successfully' })
  @ApiResponse({ status: 404, description: 'Menu action not found' })
  delete(@Param('id') id: number) {
    return this.menuActionsService.delete(id);
  }
}
