import { IsString, IsIn, IsOptional, IsNumberString, IsInt } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';

// ==================== C2C 订单类型和状态 ====================

export enum C2cOrderType {
  BUY = 'BUY',   // 买入积分股（用积分值换积分股）
  SELL = 'SELL', // 卖出积分股（用积分股换积分值）
}

export enum C2cOrderStatus {
  PENDING = 'PENDING',     // 待接单
  MATCHED = 'MATCHED',     // 已匹配（等待付款）
  PAID = 'PAID',           // 已付款（等待确认收款）
  COMPLETED = 'COMPLETED', // 已完成
  CANCELLED = 'CANCELLED', // 已取消
  EXPIRED = 'EXPIRED',     // 已过期
}

// 收款方式
export enum C2cPaymentMethod {
  GREEN_POINTS = 'GREEN_POINTS', // 绿积分（默认）
  ALIPAY = 'ALIPAY',   // 支付宝
  WECHAT = 'WECHAT',   // 微信
  BANK = 'BANK',       // 银行卡
}

// ==================== 请求 DTO ====================

export class CreateC2cOrderDto {
  @ApiProperty({ enum: ['BUY', 'SELL'], description: '订单类型' })
  @IsIn(['BUY', 'SELL'])
  type: 'BUY' | 'SELL';

  @ApiProperty({ description: '单价' })
  @IsNumberString()
  price: string;

  @ApiProperty({ description: '数量（积分股）' })
  @IsNumberString()
  quantity: string;

  @ApiPropertyOptional({ description: '最小交易量' })
  @IsOptional()
  @IsNumberString()
  minAmount?: string;

  @ApiPropertyOptional({ description: '最大交易量' })
  @IsOptional()
  @IsNumberString()
  maxAmount?: string;

  // 收款信息（卖单可选，支持逗号分隔多种方式如 'ALIPAY,WECHAT'）
  @ApiPropertyOptional({ description: '收款方式（ALIPAY/WECHAT/BANK，可逗号分隔）' })
  @IsOptional()
  @IsString()
  paymentMethod?: string;

  @ApiPropertyOptional({ description: '收款账号' })
  @IsOptional()
  @IsString()
  paymentAccount?: string;

  @ApiPropertyOptional({ description: '收款二维码URL' })
  @IsOptional()
  @IsString()
  paymentQrCode?: string;

  @ApiPropertyOptional({ description: '收款人实名' })
  @IsOptional()
  @IsString()
  paymentRealName?: string;

  @ApiPropertyOptional({ description: '备注' })
  @IsOptional()
  @IsString()
  remark?: string;
}

export class TakeC2cOrderDto {
  @ApiPropertyOptional({ description: '交易数量（可选，默认全部）' })
  @IsOptional()
  @IsNumberString()
  quantity?: string;

  // 买单被接单时，买家需要提供收款信息
  @ApiPropertyOptional({ description: '收款方式（GREEN_POINTS/ALIPAY/WECHAT/BANK，可逗号分隔）' })
  @IsOptional()
  @IsString()
  paymentMethod?: string;

  @ApiPropertyOptional({ description: '收款账号（接买单时需提供）' })
  @IsOptional()
  @IsString()
  paymentAccount?: string;

  @ApiPropertyOptional({ description: '收款二维码URL' })
  @IsOptional()
  @IsString()
  paymentQrCode?: string;

  @ApiPropertyOptional({ description: '收款人实名（接买单时需提供）' })
  @IsOptional()
  @IsString()
  paymentRealName?: string;
}

export class QueryC2cOrdersDto {
  @ApiPropertyOptional({ enum: ['BUY', 'SELL'], description: '订单类型' })
  @IsOptional()
  @IsIn(['BUY', 'SELL'])
  type?: 'BUY' | 'SELL';

  @ApiPropertyOptional({ description: '页码', default: 1 })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  page?: number;

  @ApiPropertyOptional({ description: '每页数量', default: 20 })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  pageSize?: number;
}

export class QueryMyC2cOrdersDto {
  @ApiPropertyOptional({
    enum: ['PENDING', 'MATCHED', 'PAID', 'COMPLETED', 'CANCELLED', 'EXPIRED'],
    description: '订单状态',
  })
  @IsOptional()
  @IsIn(['PENDING', 'MATCHED', 'PAID', 'COMPLETED', 'CANCELLED', 'EXPIRED'])
  status?: string;

  @ApiPropertyOptional({ description: '页码', default: 1 })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  page?: number;

  @ApiPropertyOptional({ description: '每页数量', default: 20 })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  pageSize?: number;
}

// ==================== 响应 DTO ====================

export class C2cOrderResponseDto {
  orderNo: string;
  type: string;
  status: string;
  makerAccountSequence: string;
  makerPhone?: string;
  makerNickname?: string;
  takerAccountSequence?: string;
  takerPhone?: string;
  takerNickname?: string;
  price: string;
  quantity: string;
  totalAmount: string;
  minAmount: string;
  maxAmount: string;
  // 收款信息
  paymentMethod?: string;
  paymentAccount?: string;
  paymentQrCode?: string;
  paymentRealName?: string;
  // 付款水单
  paymentProofUrl?: string;
  // 卖家 Kava 地址（绿积分转账地址）
  sellerKavaAddress?: string;
  // 超时信息
  paymentTimeoutMinutes: number;
  confirmTimeoutMinutes: number;
  paymentDeadline?: Date;
  confirmDeadline?: Date;
  // 其他
  remark?: string;
  createdAt: Date;
  matchedAt?: Date;
  paidAt?: Date;
  completedAt?: Date;
  expiredAt?: Date;
}

export class C2cOrdersPageResponseDto {
  data: C2cOrderResponseDto[];
  total: number;
  page: number;
  pageSize: number;
}
