import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Query,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { QueryBus } from '@nestjs/cqrs';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiBody, ApiParam, ApiQuery } from '@nestjs/swagger';
import { JwtAuthGuard } from '@modules/auth';
import { EndpointRateLimit, EndpointRateLimitGuard } from '@modules/shared';
import { RequireQuota, QuotaGuard } from '@modules/subscriptions';
import { type BatchValuationDto as BatchValuationResultDto } from '../../application/queries/batch-valuation/batch-valuation.handler';
import { BatchValuationQuery } from '../../application/queries/batch-valuation/batch-valuation.query';
import { type IndustrialValuationDto as IndustrialValuationResultDto } from '../../application/queries/industrial-valuation/industrial-valuation.handler';
import { IndustrialValuationQuery } from '../../application/queries/industrial-valuation/industrial-valuation.query';
import { type ValuationComparisonDto as ValuationComparisonResultDto } from '../../application/queries/valuation-comparison/valuation-comparison.handler';
import { ValuationComparisonQuery } from '../../application/queries/valuation-comparison/valuation-comparison.query';
import { type ValuationExplanationDto as ValuationExplanationResultDto } from '../../application/queries/valuation-explanation/valuation-explanation.handler';
import { ValuationExplanationQuery } from '../../application/queries/valuation-explanation/valuation-explanation.query';
import { type ValuationHistoryDto as ValuationHistoryResultDto } from '../../application/queries/valuation-history/valuation-history.handler';
import { ValuationHistoryQuery } from '../../application/queries/valuation-history/valuation-history.query';
import { AvmCompareQueryDto } from '../dto/avm-compare-query.dto';
import { AvmExplainQueryDto } from '../dto/avm-explain-query.dto';
import { BatchValuationDto } from '../dto/batch-valuation.dto';
import { IndustrialValuationDto } from '../dto/industrial-valuation.dto';
import { CacheMetaInterceptor } from '../interceptors/cache-meta.interceptor';
import { ValuationHistoryDto } from '../dto/valuation-history.dto';

@ApiTags('avm')
@UseInterceptors(CacheMetaInterceptor)
@Controller('avm')
export class AvmController {
  constructor(
    private readonly queryBus: QueryBus,
  ) {}

  @ApiBearerAuth('JWT')
  @EndpointRateLimit({ limit: 10, windowSeconds: 60, keyStrategy: 'user' })
  @UseGuards(EndpointRateLimitGuard, JwtAuthGuard, QuotaGuard)
  @RequireQuota('analytics_queries')
  @Post('batch')
  @ApiOperation({ summary: 'Batch valuation for multiple properties (max 50)' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        propertyIds: {
          type: 'array',
          minItems: 1,
          maxItems: 50,
          items: { type: 'string' },
          example: ['prop-1', 'prop-2'],
          description: 'Array of property IDs to valuate (1-50)',
        },
      },
      required: ['propertyIds'],
    },
  })
  @ApiResponse({ status: 200, description: 'Batch valuation results' })
  @ApiResponse({ status: 400, description: 'Invalid parameters' })
  @ApiResponse({ status: 401, description: 'Chưa xác thực' })
  @ApiResponse({ status: 403, description: 'Quota exceeded' })
  @ApiResponse({ status: 429, description: 'Rate limit exceeded — max 10 requests per 60s' })
  async batchValuation(@Body() dto: BatchValuationDto): Promise<BatchValuationResultDto> {
    return this.queryBus.execute(
      new BatchValuationQuery(dto.propertyIds),
    );
  }

  @ApiBearerAuth('JWT')
  @UseGuards(JwtAuthGuard, QuotaGuard)
  @RequireQuota('analytics_queries')
  @Get('history/:propertyId')
  @ApiOperation({ summary: 'Get valuation history for a property (time-series)' })
  @ApiParam({ name: 'propertyId', description: 'Property ID', example: 'prop-123' })
  @ApiResponse({ status: 200, description: 'Valuation history time-series data' })
  @ApiResponse({ status: 403, description: 'Quota exceeded' })
  async getHistory(
    @Param('propertyId') propertyId: string,
    @Query() dto: ValuationHistoryDto,
  ): Promise<ValuationHistoryResultDto> {
    return this.queryBus.execute(
      new ValuationHistoryQuery(propertyId, dto.limit ?? 50),
    );
  }

  @ApiBearerAuth('JWT')
  @EndpointRateLimit({ limit: 10, windowSeconds: 60, keyStrategy: 'user' })
  @UseGuards(EndpointRateLimitGuard, JwtAuthGuard, QuotaGuard)
  @RequireQuota('analytics_queries')
  @Get('compare')
  @ApiOperation({ summary: 'Compare valuations for 2-5 properties side by side' })
  @ApiQuery({
    name: 'ids',
    description: 'Comma-separated property IDs (2-5)',
    example: 'prop-1,prop-2,prop-3',
    type: String,
  })
  @ApiResponse({ status: 200, description: 'Normalized comparison data for UI' })
  @ApiResponse({ status: 400, description: 'Invalid parameters — provide 2-5 property IDs' })
  @ApiResponse({ status: 403, description: 'Quota exceeded' })
  @ApiResponse({ status: 429, description: 'Rate limit exceeded — max 10 requests per 60s' })
  async compare(@Query() dto: AvmCompareQueryDto): Promise<ValuationComparisonResultDto> {
    return this.queryBus.execute(
      new ValuationComparisonQuery(dto.ids),
    );
  }

  @ApiBearerAuth('JWT')
  @UseGuards(JwtAuthGuard, QuotaGuard)
  @RequireQuota('analytics_queries')
  @Get('explain')
  @ApiOperation({ summary: 'Explain a stored valuation — top drivers, comparables, confidence' })
  @ApiQuery({
    name: 'valuationId',
    description: 'Stored valuation ID to explain',
    example: 'val-abc123',
    type: String,
  })
  @ApiResponse({ status: 200, description: 'Valuation explanation with drivers and comparables' })
  @ApiResponse({ status: 400, description: 'Invalid parameters' })
  @ApiResponse({ status: 403, description: 'Quota exceeded' })
  @ApiResponse({ status: 404, description: 'Valuation not found' })
  async explain(@Query() dto: AvmExplainQueryDto): Promise<ValuationExplanationResultDto> {
    return this.queryBus.execute(
      new ValuationExplanationQuery(dto.valuationId),
    );
  }

  @ApiBearerAuth('JWT')
  @EndpointRateLimit({ limit: 10, windowSeconds: 60, keyStrategy: 'user' })
  @UseGuards(EndpointRateLimitGuard, JwtAuthGuard, QuotaGuard)
  @RequireQuota('analytics_queries')
  @Post('industrial')
  @ApiOperation({ summary: 'Estimate industrial property rent using AI model' })
  @ApiResponse({ status: 200, description: 'Industrial rent estimation with comparables and drivers' })
  @ApiResponse({ status: 400, description: 'Invalid parameters' })
  @ApiResponse({ status: 403, description: 'Quota exceeded' })
  @ApiResponse({ status: 429, description: 'Rate limit exceeded — max 10 requests per 60s' })
  async industrialValuation(@Body() dto: IndustrialValuationDto): Promise<IndustrialValuationResultDto> {
    return this.queryBus.execute(
      new IndustrialValuationQuery(
        dto.province,
        dto.region,
        dto.parkOccupancyRate,
        dto.parkAreaHa,
        dto.parkAgeYears,
        dto.distanceToPortKm,
        dto.distanceToAirportKm,
        dto.distanceToHighwayKm,
        dto.propertyType,
        dto.areaM2,
        dto.ceilingHeightM,
        dto.floorLoadTonM2,
        dto.powerCapacityKva,
        dto.buildingCoverage,
        dto.loadingDocks,
        dto.zoning,
        dto.industryDemandIndex,
        dto.fdiProvinceMusd,
        dto.laborCostProvinceVnd,
        dto.logisticsConnectivityScore,
      ),
    );
  }
}
