import { ApiProperty } from '@nestjs/swagger';

export class SnapshotDetailResponse {
  @ApiProperty() id: string;
  @ApiProperty() target: string;
  @ApiProperty() status: string;
  @ApiProperty() progress: number;
  @ApiProperty({ required: false }) progressMsg: string | null;
  @ApiProperty() fileSize: string;
  @ApiProperty() fileName: string | null;
  @ApiProperty() error: string | null;
  @ApiProperty() startedAt: Date | null;
  @ApiProperty() completedAt: Date | null;
}

export class SnapshotTaskResponse {
  @ApiProperty() id: string;
  @ApiProperty() status: string;
  @ApiProperty() storageType: string;
  @ApiProperty() storagePath: string;
  @ApiProperty() targets: string[];
  @ApiProperty() totalSize: string;
  @ApiProperty() error: string | null;
  @ApiProperty() startedAt: Date | null;
  @ApiProperty() completedAt: Date | null;
  @ApiProperty() createdAt: Date;
  @ApiProperty({ type: [SnapshotDetailResponse] })
  details: SnapshotDetailResponse[];
}

export class SnapshotListResponse {
  @ApiProperty({ type: [SnapshotTaskResponse] }) tasks: SnapshotTaskResponse[];
  @ApiProperty() total: number;
  @ApiProperty() page: number;
  @ApiProperty() limit: number;
}

/** 将 Prisma 对象转换为响应 DTO（处理 BigInt 序列化） */
export function toSnapshotResponse(task: any): SnapshotTaskResponse {
  return {
    id: task.id,
    status: task.status,
    storageType: task.storageType,
    storagePath: task.storagePath,
    targets: JSON.parse(task.targets),
    totalSize: task.totalSize.toString(),
    error: task.error,
    startedAt: task.startedAt,
    completedAt: task.completedAt,
    createdAt: task.createdAt,
    details: (task.details || []).map((d: any) => ({
      id: d.id,
      target: d.target,
      status: d.status,
      progress: d.progress,
      progressMsg: d.progressMsg ?? null,
      fileSize: d.fileSize.toString(),
      fileName: d.fileName,
      error: d.error,
      startedAt: d.startedAt,
      completedAt: d.completedAt,
    })),
  };
}
