import { Transform, Type } from 'class-transformer';
import {
  IsBoolean,
  IsIn,
  IsOptional,
  IsString,
  IsUUID,
  Length,
  Matches,
} from 'class-validator';

export class UpsertNovasisPayConfigDto {
  @IsOptional()
  @IsString()
  @Length(1, 64)
  merchant_id?: string;

  @IsOptional()
  @IsString()
  @Matches(/^pk_(test|live)_[A-Za-z0-9_-]{8,}$/, {
    message: 'publishable_key debe tener formato pk_test_… o pk_live_…',
  })
  publishable_key?: string;

  // sk_test_… / sk_live_… — sólo viaja al guardar/rotar. Nunca se devuelve.
  @IsOptional()
  @IsString()
  @Matches(/^sk_(test|live)_[A-Za-z0-9_-]{8,}$/, {
    message: 'secret_key debe tener formato sk_test_… o sk_live_…',
  })
  secret_key?: string;

  @IsOptional()
  @IsString()
  @Length(8, 256)
  webhook_secret?: string;

  @IsOptional()
  @IsIn(['test', 'live'])
  mode?: 'test' | 'live';

  @IsOptional()
  @IsString()
  @Length(2, 40)
  default_provider?: string;

  @IsOptional()
  @IsString()
  @Length(2, 2)
  default_country?: string;

  @IsOptional()
  @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
  @Matches(/^https?:\/\/\S+$/, {
    message: 'api_base_url debe ser una URL http(s) (ej.: https://api.pay.novasis.com/v1)',
  })
  api_base_url?: string;

  @IsOptional()
  @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
  @Matches(/^https?:\/\/\S+$/, {
    message: 'checkout_base_url debe ser una URL http(s) (ej.: https://pay.novasis.com/checkout)',
  })
  checkout_base_url?: string;

  @IsOptional()
  @IsUUID()
  cuenta_contable_por_liquidar?: string | null;

  @IsOptional()
  @IsUUID()
  cuenta_contable_banco?: string | null;

  @IsOptional()
  @IsUUID()
  cuenta_contable_comision?: string | null;

  @IsOptional()
  @Type(() => Boolean)
  @IsBoolean()
  activo?: boolean;

  // Si true, los pagos con QR directo se muestran embebidos en nuestra UI en
  // lugar de redirigir a la página del proveedor. Config avanzada por empresa.
  @IsOptional()
  @Type(() => Boolean)
  @IsBoolean()
  qr_embebido?: boolean;
}
