import { ApiProperty } from "@nestjs/swagger";
import { Provider } from "@prisma/client";
import {
  IsEnum,
  IsInt,
  IsISO8601,
  IsObject,
  IsOptional,
  IsPositive,
  IsString,
  Length,
  Matches,
} from "class-validator";

export class CreatePaymentIntentDto {
  @ApiProperty({
    example: 150000,
    description:
      "Monto en la unidad menor de la moneda, salvo monedas sin decimales (PYG). " +
      "Para PYG enviar enteros (`150000` = 150.000 Gs). Para USD enviar centavos (`12500` = USD 125,00).",
  })
  @IsInt()
  @IsPositive()
  amount!: number;

  @ApiProperty({ example: "PYG", description: "ISO 4217, 3 letras mayúsculas." })
  @IsString()
  @Matches(/^[A-Z]{3}$/, { message: "currency must be uppercase ISO 4217" })
  currency!: string;

  @ApiProperty({ example: "PY", description: "ISO 3166-1 alpha-2 del comprador." })
  @IsString()
  @Matches(/^[A-Z]{2}$/, { message: "country must be uppercase ISO 3166-1 alpha-2" })
  country!: string;

  @ApiProperty({
    required: false,
    example: "ORDER-2026-0123",
    description: "Referencia externa del merchant (orden, factura, etc.). Único informativo.",
  })
  @IsOptional()
  @IsString()
  @Length(1, 120)
  externalReference?: string;

  @ApiProperty({ required: false, example: "Pago de orden #0123" })
  @IsOptional()
  @IsString()
  @Length(1, 500)
  description?: string;

  @ApiProperty({
    required: false,
    example: "cus_01HXYZABCDEFGHJKMNPQRSTV",
    description: "ID de un customer previamente creado en este merchant.",
  })
  @IsOptional()
  @IsString()
  customerId?: string;

  @ApiProperty({
    required: false,
    enum: Provider,
    description: "Forzar provider específico. Si se omite, el gateway rutea según prioridad.",
  })
  @IsOptional()
  @IsEnum(Provider)
  requestedProvider?: Provider;

  @ApiProperty({
    required: false,
    example: "18",
    description:
      "Medio de pago pre-seleccionado, en el código propio del provider (ej. `platformId` de Dpago: 18 = QR Ueno). " +
      "Si se envía, la confirmación cobra directo ese método (útil para pedir QR sin link). Se resuelve contra el " +
      "catálogo `provider_payment_method`. Se puede sobrescribir al confirmar.",
  })
  @IsOptional()
  @IsString()
  platformId?: string;

  @ApiProperty({
    required: false,
    example: "2026-06-08T14:30:00.000Z",
    description: "Fecha ISO-8601 de expiración del intent. Default: configurado a nivel sistema.",
  })
  @IsOptional()
  @IsISO8601()
  expiresAt?: string;

  @ApiProperty({ required: false, example: { orderId: "ORDER-2026-0123" } })
  @IsOptional()
  @IsObject()
  metadata?: Record<string, unknown>;
}
