import { Body, Controller, Post } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger';
import { AuthService } from './services/auth.service';
import { LoginUserDto } from './dto/request/logins.dto';
import { LoginResponseDto } from './dto/response/login-response.dto';

/**
 * AuthController - Authentication controller for user login operations
 *
 * This controller handles user authentication endpoints, specifically the login flow.
 * It validates user credentials, generates JWT tokens, and returns authentication
 * response in a standardized format.
 *
 * Routes are prefixed with '/user' as defined in the @Controller decorator.
 *
 * @example
 * // Login request
 * POST /user/login
 * Body: { "email": "user@example.com", "password": "password123" }
 *
 * @example
 * // Login response
 * {
 *   "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
 *   "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
 *   "expires_in": 3600,
 *   "id": 12345,
 *   "email_verified_at": "2024-01-15T10:30:00Z",
 *   "role": "user",
 *   "role_id": 2
 * }
 */
@ApiTags('Auth') // Groups endpoints under 'Auth' in Swagger UI
@Controller('user') // Base route path: /user
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  /**
   * User login endpoint
   *
   * Authenticates user credentials and returns JWT tokens for session management.
   * Flow:
   * 1. Validate login credentials via AuthService
   * 2. Generate JWT access and refresh tokens
   * 3. Return authentication response with user metadata
   *
   * @param loginDto - User credentials (email and password)
   * @returns Authentication response with tokens and user information
   *
   * @throws 404 - User not found
   * @throws 401 - Account inactive
   * @throws 403 - Invalid password
   *
   * @note Consider implementing rate limiting to prevent brute force attacks
   * @note All validation errors return generic messages to prevent user enumeration
   */
  @Post('login')
  @ApiOperation({
    summary: 'User login',
    description:
      'Authenticate user with email and password, returns JWT tokens',
  })
  @ApiBody({ type: LoginUserDto }) // Swagger request body documentation
  @ApiResponse({
    status: 200,
    description: 'Login successful',
    type: LoginResponseDto, // Consider creating proper response DTO
  })
  @ApiResponse({ status: 404, description: 'User not found' })
  @ApiResponse({ status: 401, description: 'Account inactive' })
  @ApiResponse({ status: 403, description: 'Invalid credentials' })
  async login(@Body() loginDto: LoginUserDto): Promise<any> {
    // Step 1: Authenticate user credentials
    const user = await this.authService.authCheck(loginDto);

    // Step 2: Generate JWT tokens for authenticated session
    const jwtToken = await this.authService.generateToken(user);

    // Step 3: Return complete authentication response
    return {
      ...jwtToken, // Spread token properties (access_token, refresh_token, expires_in)
      id: user.id, // User identifier
      email_verified_at: user.mail_sent, // Email verification status/timestamp
      role: 'user.Role', // TODO: Replace with actual user role from user entity
      role_id: '2', // TODO: Replace with actual role ID from user entity
    };
  }
}
