import { ApiProperty } from "@nestjs/swagger";
import { BillingCycle } from "@prisma/client";
import { Transform } from "class-transformer";
import {
  IsBoolean,
  IsEnum,
  IsInt,
  IsNumber,
  IsOptional,
  IsString,
  Length,
  Matches,
  Min,
} from "class-validator";

export class CreatePricingPlanDto {
  @ApiProperty({
    example: "Plan Estándar PY 2026",
    description: "Etiqueta interna del plan (visible solo en admin).",
  })
  @IsString()
  @Length(2, 120)
  name!: string;

  @ApiProperty({
    example: 150000,
    description: "Fee mensual fijo (en la moneda del plan). 0 = sin fee mensual.",
  })
  @IsNumber({ maxDecimalPlaces: 4 })
  @Min(0)
  monthlyFee!: number;

  @ApiProperty({ example: "PYG", default: "PYG" })
  @IsOptional()
  @IsString()
  @Length(3, 3)
  @Matches(/^[A-Z]{3}$/)
  @Transform(({ value }) => (typeof value === "string" ? value.toUpperCase() : value))
  monthlyFeeCurrency?: string;

  @ApiProperty({
    example: 500,
    description: "Fee fijo por transacción aprobada (en la moneda del plan).",
  })
  @IsNumber({ maxDecimalPlaces: 4 })
  @Min(0)
  txFeeFixed!: number;

  @ApiProperty({
    example: 0.3,
    description: "Fee porcentual sobre monto aprobado. 0.3 = 0,3%.",
  })
  @IsNumber({ maxDecimalPlaces: 4 })
  @Min(0)
  txFeePercentage!: number;

  @ApiProperty({ example: "PYG", default: "PYG" })
  @IsOptional()
  @IsString()
  @Length(3, 3)
  @Matches(/^[A-Z]{3}$/)
  @Transform(({ value }) => (typeof value === "string" ? value.toUpperCase() : value))
  txFeeCurrency?: string;

  @ApiProperty({
    required: false,
    example: 200,
    description:
      "Tx aprobadas incluidas en el fee mensual. A partir de este número, se cobra `overQuotaTxFee` por tx adicional.",
  })
  @IsOptional()
  @IsInt()
  @Min(0)
  includedTxPerMonth?: number;

  @ApiProperty({
    required: false,
    example: 800,
    description: "Costo por tx que excede la cuota incluida.",
  })
  @IsOptional()
  @IsNumber({ maxDecimalPlaces: 4 })
  @Min(0)
  overQuotaTxFee?: number;

  @ApiProperty({
    enum: BillingCycle,
    default: BillingCycle.monthly,
    required: false,
    description:
      "Frecuencia de facturación. `monthly` factura todos los meses; `quarterly` cada cierre de trimestre (mar/jun/sep/dic); `yearly` solo en diciembre. `monthlyFee` se multiplica por la cantidad de meses del ciclo.",
  })
  @IsOptional()
  @IsEnum(BillingCycle)
  billingCycle?: BillingCycle;

  @ApiProperty({
    required: false,
    default: false,
    description:
      "Si es `true`, el plan no genera facturas (comercio bonificado o cortesía).",
  })
  @IsOptional()
  @IsBoolean()
  free?: boolean;

  @ApiProperty({
    required: false,
    default: false,
    description:
      "Si es `true`, al generar la factura se crea un PaymentIntent en el merchant interno de Novasis para que el comercio pague el fee con su propio método. Requiere `SELF_BILLING_MERCHANT_ID` configurado.",
  })
  @IsOptional()
  @IsBoolean()
  selfBillingEnabled?: boolean;
}
