/**
 * 统计相关 DTOs
 */

import { IsString, IsOptional, IsEnum, IsDateString } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

/**
 * 时间范围枚举
 */
export enum TimeRange {
  LAST_7_DAYS = '7d',
  LAST_30_DAYS = '30d',
  LAST_90_DAYS = '90d',
  LAST_1_YEAR = '1y',
}

/**
 * 统计查询 DTO
 */
export class StatisticsQueryDto {
  @ApiPropertyOptional({
    description: '时间范围',
    enum: TimeRange,
    default: TimeRange.LAST_30_DAYS,
    example: '30d',
  })
  @IsOptional()
  @IsEnum(TimeRange, { message: '时间范围必须是 7d, 30d, 90d, 1y 之一' })
  timeRange?: TimeRange = TimeRange.LAST_30_DAYS;

  @ApiPropertyOptional({
    description: '开始时间 (ISO 8601 格式，优先级高于 timeRange)',
    example: '2024-01-01T00:00:00Z',
  })
  @IsOptional()
  @IsDateString()
  startDate?: string;

  @ApiPropertyOptional({
    description: '结束时间 (ISO 8601 格式)',
    example: '2024-12-31T23:59:59Z',
  })
  @IsOptional()
  @IsDateString()
  endDate?: string;

  @ApiPropertyOptional({
    description: '表单分类筛选',
    example: 'finance',
  })
  @IsOptional()
  @IsString()
  category?: string;
}

/**
 * 统计响应 - 总体统计
 */
export class FormStatisticsDto {
  @ApiProperty({ description: '总提交数', example: 1245 })
  totalSubmissions: number;

  @ApiProperty({ description: '总表单数', example: 25 })
  totalForms: number;

  @ApiProperty({ description: '已发布表单数', example: 20 })
  activeForms: number;

  @ApiProperty({ description: '草稿表单数', example: 5 })
  draftForms: number;

  @ApiProperty({ description: '平均处理时间 (格式化字符串)', example: '2.5 小时' })
  avgProcessingTime: string;

  @ApiProperty({ description: '平均处理时间 (毫秒)', example: 9000000 })
  avgProcessingTimeMs: number;

  @ApiProperty({ description: '审批通过率 (百分比)', example: 92 })
  approvalRate: number;

  @ApiProperty({ description: '活跃用户数', example: 156 })
  activeUsers: number;
}

/**
 * 提交趋势数据点
 */
export class SubmissionTrendDto {
  @ApiProperty({ description: '日期', example: '2024-12-01' })
  date: string;

  @ApiProperty({ description: '提交数', example: 42 })
  submissions: number;

  @ApiProperty({ description: '通过数', example: 38 })
  approvals: number;

  @ApiProperty({ description: '驳回数', example: 4 })
  rejections: number;
}

/**
 * 热门表单
 */
export class TopFormDto {
  @ApiProperty({ description: '表单定义 ID', example: 'fd-001' })
  formDefinitionId: string;

  @ApiProperty({ description: '表单名称', example: '报销申请' })
  formName: string;

  @ApiProperty({ description: '表单标识', example: 'expense_claim' })
  slug: string;

  @ApiProperty({ description: '提交次数', example: 342 })
  submissions: number;

  @ApiProperty({ description: '通过次数', example: 315 })
  approvals: number;

  @ApiProperty({ description: '驳回次数', example: 27 })
  rejections: number;

  @ApiProperty({ description: '审批通过率 (百分比)', example: 92 })
  approvalRate: number;

  @ApiProperty({ description: '平均处理时间 (毫秒)', example: 7200000 })
  avgProcessingTime: number;
}

/**
 * 分类分布
 */
export class CategoryDistributionDto {
  @ApiProperty({ description: '分类名称', example: '财务' })
  category: string;

  @ApiProperty({ description: '数量', example: 15 })
  count: number;

  @ApiProperty({ description: '百分比', example: 35 })
  percentage: number;
}

/**
 * 统计响应 - 完整响应
 */
export class StatisticsResponseDto {
  @ApiProperty({ description: '总体统计', type: FormStatisticsDto })
  stats: FormStatisticsDto;

  @ApiProperty({ description: '热门表单排行', type: [TopFormDto] })
  topForms: TopFormDto[];

  @ApiProperty({ description: '提交趋势', type: [SubmissionTrendDto] })
  trends: SubmissionTrendDto[];

  @ApiProperty({ description: '分类分布', type: [CategoryDistributionDto] })
  categoryDistribution: CategoryDistributionDto[];

  @ApiProperty({
    description: '时间范围',
    example: { from: '2024-11-01T00:00:00Z', to: '2024-12-01T00:00:00Z' },
  })
  timeRange: {
    from: string;
    to: string;
  };
}

