/**
 * 快照相关 DTOs
 */

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

/**
 * 快照状态
 * 简化流程：审核通过 = 发布（PENDING → ACTIVE/REJECTED）
 */
export enum SnapshotStatus {
  DRAFT = 'DRAFT',
  PENDING = 'PENDING',
  REJECTED = 'REJECTED',
  ACTIVE = 'ACTIVE',
  ARCHIVED = 'ARCHIVED',
}

/**
 * 提交审核 DTO
 */
export class SubmitReviewDto {
  @ApiPropertyOptional({
    description: '提交说明',
    example: '第一版表单，请审核',
  })
  @IsOptional()
  @IsString()
  comment?: string;
}

/**
 * 审核操作类型
 */
export enum ReviewAction {
  APPROVE = 'APPROVE',
  REJECT = 'REJECT',
}

/**
 * 审核 DTO
 */
export class ReviewSnapshotDto {
  @ApiProperty({
    description: '审核操作',
    enum: ReviewAction,
    example: 'APPROVE',
  })
  @IsNotEmpty()
  @IsEnum(ReviewAction)
  action: ReviewAction;

  @ApiPropertyOptional({
    description: '审核意见',
    example: '审核通过，符合规范',
  })
  @IsOptional()
  @IsString()
  comment?: string;
}

/**
 * 发布 DTO
 */
export class PublishSnapshotDto {
  @ApiPropertyOptional({
    description: '发布说明',
    example: '首次发布，包含基础报销功能',
  })
  @IsOptional()
  @IsString()
  releaseNote?: string;
}

/**
 * 提交审核响应
 */
export class SubmitReviewResponse {
  @ApiProperty() snapshotId: string;
  @ApiProperty() formVersionId: string;
  @ApiProperty() processVersionId: string;
  @ApiProperty() status: string;
  @ApiProperty() submittedAt: string;
}

/**
 * 审核响应
 * 审核通过后会自动设置为 PUBLISHED 和默认版本
 */
export class ReviewResponse {
  @ApiProperty() snapshotId: string;
  @ApiProperty({ description: '状态：PUBLISHED（通过）或 REJECTED（驳回）' }) status: string;
  @ApiPropertyOptional({ description: '是否为默认版本（仅通过时返回）' }) isDefault?: boolean;
  @ApiProperty() reviewedAt: string;
  @ApiProperty() reviewedBy: string;
  @ApiPropertyOptional() reviewComment?: string;
  @ApiPropertyOptional({ description: '发布时间（仅通过时返回）' }) publishedAt?: string;
}

/**
 * 发布响应
 */
export class PublishResponse {
  @ApiProperty() snapshotId: string;
  @ApiProperty() status: string;
  @ApiPropertyOptional() releaseNote?: string;
  @ApiProperty() publishedAt: string;
  @ApiProperty() publishedBy: string;
  @ApiPropertyOptional() previousSnapshotId?: string;
  @ApiProperty() formDefinitionStatus: string;
}

/**
 * 快照详情
 */
export class SnapshotDetail {
  @ApiProperty() snapshotId: string;
  @ApiProperty() regionId: string;
  @ApiProperty() formDefinitionId: string;
  @ApiProperty() formVersion: {
    id: string;
    version: number;
    versionName?: string;
    schema: any;
    uiSchema?: any;
  };
  @ApiProperty() processVersion: {
    id: string;
    version: number;
    versionName?: string;
    model: any;
  };
  @ApiProperty() status: string;
  @ApiPropertyOptional() releaseNote?: string;
  @ApiPropertyOptional() publishedAt?: string;
  @ApiPropertyOptional() publishedBy?: string;
  @ApiProperty() createdAt: string;
}

/**
 * 快照列表项
 */
export class SnapshotListItem {
  @ApiProperty() snapshotId: string;
  @ApiProperty() formVersionId: string;
  @ApiProperty() formVersion: number;
  @ApiProperty() processVersionId: string;
  @ApiProperty() processVersion: number;
  @ApiProperty() status: string;
  @ApiPropertyOptional() releaseNote?: string;
  @ApiPropertyOptional() publishedAt?: string;
  @ApiProperty() createdAt: string;
}

/**
 * 待审核列表项
 */
export class PendingReviewItem {
  @ApiProperty() snapshotId: string;
  @ApiProperty() formDefinitionId: string;
  @ApiProperty() formDefinitionName: string;
  @ApiProperty() formVersionId: string;
  @ApiProperty() formVersion: number;
  @ApiProperty() submittedAt: string;
  @ApiProperty() submittedBy: string; // 提交人显示名称
  @ApiProperty() submittedById: string; // 提交人ID（用于筛选）
  @ApiPropertyOptional() comment?: string;
}
