import {
  Controller,
  Post,
  Body,
  Req,
  UseGuards,
  BadRequestException,
} from '@nestjs/common';
import { FeatureUsageService } from './feature-usage.service';
// import { JwtAuthGuard } from '../../auth/jwt-auth.guard';

@Controller('features')
export class FeatureUsageController {
  constructor(private featureUsageService: FeatureUsageService) {}

  @Post('unlock-candidate')
  // @UseGuards(JwtAuthGuard)
  async unlockCandidate(@Req() req, @Body() body: any) {
    // ---------------------------------------------------------
    // 1. Manual Validation (NO DTOs)
    // ---------------------------------------------------------
    if (!body || typeof body !== 'object') {
      throw new BadRequestException('Invalid request body');
    }

    // Validate candidateId (which we treat as the referenceId)
    const candidateId = Number(body.candidateId);
    if (isNaN(candidateId) || candidateId <= 0) {
      throw new BadRequestException(
        'Valid candidateId is required and must be a number',
      );
    }

    const userId = req.user?.id || 1; // Mock ID

    // ---------------------------------------------------------
    // 2. Execute Service
    // ---------------------------------------------------------
    return await this.featureUsageService.unlockFeature(
      userId,
      'view_contact', // Hardcoded Feature Key for this specific endpoint
      candidateId,
    );
  }
}
