// Import necessary decorators and utilities from NestJS
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  ValidationPipe,
} from '@nestjs/common';

// Import Swagger decorator for API documentation
import { ApiTags } from '@nestjs/swagger';

// Import the service containing business logic
import { UserSettingsService } from './user-settings.service';

// Import DTOs for request validation
import { CreateUserSettingsDto } from './dto/create.dto';
import { UserSettingsEntity } from 'src/entities/UserSettings.entity';
import { SearchUserSettingsDto } from './dto/search-dto';
import { UpdateUserSettingsDto } from './dto/update.dto';

// Import custom pagination response type
import { Response } from 'src/config/pagination/paginationDto';

// Import shared table settings service
import { TableSettingsService } from 'src/services/table-settings.service';
import { TableSettingsDto } from './dto/table-settings-dto';

/**
 * UserSettingsController - Handles HTTP requests for user settings management
 *
 * This controller provides RESTful CRUD operations for user settings along with
 * specialized endpoints for table settings management. It demonstrates clean
 * separation between general user settings and specialized table settings.
 *
 * Key Characteristics:
 * - Standard CRUD operations for user settings
 * - Integration with shared TableSettingsService
 * - RESTful design with proper HTTP methods
 * - Separation of concerns between user settings and table settings
 *
 * Business Context:
 * - Manages user-specific configuration and preferences
 * - Integrates with table/view customization system
 * - Part of user personalization and experience management
 *
 * @ApiTags('User Settings') - Groups all endpoints under "User Settings" in Swagger UI
 * @Controller('') - Base route for this controller
 */
@ApiTags('User Settings')
@Controller('')
export class UserSettingsController {
  /**
   * Constructor with dependency injection for two services
   *
   * @param groupService - Main service for user settings CRUD operations
   *                    Note: Named 'groupService' which might be a legacy name
   *                    or indicates this handles user setting groups
   * @param settingService - Shared service for table settings functionality
   */
  constructor(
    private readonly groupService: UserSettingsService,
    private readonly settingService: TableSettingsService,
  ) {}

  /**
   * POST /all - Retrieves paginated user settings with filtering
   *
   * Uses POST instead of GET to support complex search criteria in request body.
   * This is a common pattern for advanced search/filter operations.
   *
   * @param searchDto - Contains search criteria, filters, and pagination parameters
   * @returns Promise<Response<UserSettingsEntity>> - Paginated response with settings
   */
  @Post('all')
  findAll(
    @Body() searchDto: SearchUserSettingsDto,
  ): Promise<Response<UserSettingsEntity>> {
    return this.groupService.findAll(searchDto);
  }

  /**
   * GET /:id - Retrieves a single user setting by ID
   *
   * Standard RESTful endpoint for retrieving a specific resource.
   *
   * @param id - User setting ID from URL parameter
   * @returns Single user setting entity
   */
  @Get(':id')
  findOne(@Param('id') id: number) {
    return this.groupService.findOne(id);
  }

  /**
   * POST / - Creates a new user setting
   *
   * Uses ValidationPipe to automatically validate the request body
   * against the CreateUserSettingsDto validation rules.
   *
   * Note: Hardcoded user ID (2) - This is likely for demonstration,
   * testing, or should be replaced with actual user context from session.
   *
   * @param body - Validated creation data from request body
   * @returns Promise<UserSettingsEntity> - The newly created user setting
   */
  @Post()
  create(
    @Body(new ValidationPipe()) body: CreateUserSettingsDto,
  ): Promise<UserSettingsEntity> {
    return this.groupService.create(2, body); // Hardcoded user ID 2
  }

  /**
   * PUT /:id - Updates an existing user setting
   *
   * Follows RESTful convention: PUT for complete updates.
   * Uses ValidationPipe for request body validation.
   *
   * @param id - User setting ID from URL parameter
   * @param body - Validated update data from request body
   * @returns Update result
   */
  @Put(':id')
  update(
    @Param('id') id: number,
    @Body(new ValidationPipe()) body: UpdateUserSettingsDto,
  ) {
    return this.groupService.update(id, body);
  }

  /**
   * DELETE /:id - Deletes a user setting
   *
   * Standard RESTful endpoint for resource deletion.
   *
   * @param id - User setting ID to delete
   * @returns Delete result
   */
  @Delete(':id')
  delete(@Param('id') id: number) {
    return this.groupService.delete(id);
  }

  /**
   * POST /user-table-settings - Retrieves or auto-generates table settings
   *
   * Specialized endpoint for table settings management that delegates to
   * a shared TableSettingsService. This demonstrates separation of concerns
   * and service reuse across different parts of the application.
   *
   * The 'autoGetPageSettings' method likely implements smart logic:
   * 1. Check for existing user-specific table settings
   * 2. If not found, generate default settings based on page/context
   * 3. Return appropriate table configuration
   *
   * @param body - Table settings request parameters
   * @returns Table settings configuration
   */
  @Post('user-table-settings')
  async findTableSettings(@Body() body: TableSettingsDto) {
    return await this.settingService.autoGetPageSettings(body);
  }
}
