import { ApiProperty } from "@nestjs/swagger";
import { CheckoutSessionStatus } from "@prisma/client";

export class CheckoutSessionDto {
  @ApiProperty({ example: "cs_01HXYZABCDEFGHJKMNPQRSTV" })
  id!: string;

  @ApiProperty({ example: "pi_01HXYZABCDEFGHJKMNPQRSTV" })
  intentId!: string;

  @ApiProperty({
    example: "f3a9b2c1d8e7f4a3b2c1d8e7f4a3b2c1d8e7f4a3b2c",
    description: "Token público URL-safe. Se usa para construir la URL del hosted checkout: `<CHECKOUT_BASE_URL>/c/<publicToken>`.",
  })
  publicToken!: string;

  @ApiProperty({
    example: "https://checkout.novasispay.com/c/f3a9b2c1d8e7f4a3b2c1d8e7f4a3b2c1d8e7f4a3b2c",
    description: "URL completa lista para redirigir al comprador o abrir en iframe.",
  })
  url!: string;

  @ApiProperty({ enum: CheckoutSessionStatus, example: CheckoutSessionStatus.open })
  status!: CheckoutSessionStatus;

  @ApiProperty({ required: false, nullable: true, example: "https://merchant.com/orders/123/success" })
  returnUrl!: string | null;

  @ApiProperty({ required: false, nullable: true, example: "https://merchant.com/orders/123/cancel" })
  cancelUrl!: string | null;

  @ApiProperty({ required: false, nullable: true, example: { locale: "es-PY" } })
  uiConfig!: Record<string, unknown> | null;

  @ApiProperty({ example: 0 })
  failedAttempts!: number;

  @ApiProperty({ example: "2026-06-08T14:30:00.000Z" })
  expiresAt!: Date;

  @ApiProperty({ example: "2026-06-07T14:30:00.000Z" })
  createdAt!: Date;
}

export class CreatedCheckoutSessionDto extends CheckoutSessionDto {
  @ApiProperty({
    example: "cs_secret_f3a9b2c1d8e7f4a3b2c1d8e7f4a3b2c1d8e7f4a3b2c",
    description:
      "Client secret para drop-in SDK. Se devuelve **solo en la creación**. Permite operar exclusivamente sobre este intent.",
  })
  clientSecret!: string;
}

export class PublicCheckoutSessionDto {
  @ApiProperty({ example: "cs_01HXYZABCDEFGHJKMNPQRSTV" })
  id!: string;

  @ApiProperty({ enum: CheckoutSessionStatus, example: CheckoutSessionStatus.open })
  status!: CheckoutSessionStatus;

  @ApiProperty({ example: "2026-06-08T14:30:00.000Z" })
  expiresAt!: Date;

  @ApiProperty({ example: "150000.0000" })
  amount!: string;

  @ApiProperty({ example: "PYG" })
  currency!: string;

  @ApiProperty({ example: "PY" })
  country!: string;

  @ApiProperty({ required: false, nullable: true, example: "Pago de orden #0123" })
  description!: string | null;

  @ApiProperty({ example: "Acme S.A." })
  merchantName!: string;

  @ApiProperty({ required: false, nullable: true, example: { locale: "es-PY" } })
  uiConfig!: Record<string, unknown> | null;

  @ApiProperty({ required: false, nullable: true, example: "https://merchant.com/orders/123/success" })
  returnUrl!: string | null;

  @ApiProperty({ required: false, nullable: true, example: "https://merchant.com/orders/123/cancel" })
  cancelUrl!: string | null;

  @ApiProperty({
    required: false,
    nullable: true,
    description:
      "Datos del comprador cuando el merchant creó un Customer al iniciar el cobro. " +
      "El hosted checkout los usa para prellenar el form.",
    example: {
      email: "cliente@acme.com.py",
      name: "Juan Pérez",
      docNumber: "80012345-6",
      phone: "+595981000111",
      country: "PY",
    },
  })
  customer!: {
    email: string | null;
    name: string | null;
    docNumber: string | null;
    phone: string | null;
    country: string | null;
  } | null;
}
