import { applyDecorators } from '@nestjs/common';
import {
  ApiBadRequestResponse,
  ApiBearerAuth,
  ApiForbiddenResponse,
  ApiNotFoundResponse,
  ApiTooManyRequestsResponse,
  ApiUnauthorizedResponse
} from '@nestjs/swagger';

export const ESTATE_ID_EXAMPLE = '00000000-0000-4000-8000-000000000001';
export const USER_ID_EXAMPLE = 'user-uuid';
export const GUARD_ID_EXAMPLE = 'guard-uuid';
export const RESIDENT_ID_EXAMPLE = 'resident-uuid';
export const VISITOR_ID_EXAMPLE = 'visitor-uuid';
export const VISITOR_PASS_ID_EXAMPLE = 'visitor-pass-uuid';
export const VISIT_SESSION_ID_EXAMPLE = 'visit-session-uuid';

const errorSchema = (statusCode: number, error: string, message: string | string[]) => ({
  example: {
    statusCode,
    message,
    error
  }
});

export function ApiProtectedEndpoint() {
  return applyDecorators(
    ApiBearerAuth('bearer'),
    ApiUnauthorizedResponse({
      description: 'Missing, expired, or invalid access token.',
      schema: errorSchema(401, 'Unauthorized', 'Bearer token is required')
    }),
    ApiForbiddenResponse({
      description: 'Authenticated user does not have the required role or estate scope.',
      schema: errorSchema(403, 'Forbidden', 'Cross-estate access is not allowed')
    })
  );
}

export function ApiValidationError() {
  return ApiBadRequestResponse({
    description: 'Request body, query, or route parameter failed validation.',
    schema: errorSchema(400, 'Bad Request', ['estateId must be a string'])
  });
}

export function ApiNotFoundError(resource: string) {
  return ApiNotFoundResponse({
    description: `${resource} was not found or is not visible in this estate.`,
    schema: errorSchema(404, 'Not Found', `${resource} not found`)
  });
}

export function ApiAuthThrottleError() {
  return ApiTooManyRequestsResponse({
    description: 'Authentication rate limit exceeded. Wait for the throttling window to reset.',
    schema: errorSchema(429, 'Too Many Requests', 'Too many requests')
  });
}
