import { ProviderPaymentStatus } from "@edr/types";
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import { IsEnum, IsIn, IsOptional, IsString } from "class-validator";

export type PaymentPlatformDto = "web" | "mobile";

export enum PaymentMethodTypeEnum {
  TELEBIRR = "TELEBIRR",
  CBE_BIRR = "CBE_BIRR",
  EBIRR = "EBIRR",
  WAAFI = "WAAFI",
  CARD = "CARD",
  DMONEY = "DMONEY",
  CAC_BANK = "CAC_BANK",
}

export class InitiatePaymentDto {
  @ApiProperty({ example: "booking-uuid" })
  @IsString()
  bookingId!: string;

  @ApiProperty({
    enum: PaymentMethodTypeEnum,
    description: "Payment method: TELEBIRR/CBE_BIRR/EBIRR (Ethiopia), WAAFI (Djibouti), CARD (International), DMONEY",
    example: "TELEBIRR",
  })
  @IsEnum(PaymentMethodTypeEnum)
  method!: PaymentMethodTypeEnum;

  @ApiPropertyOptional({ enum: ["web", "mobile"], default: "web" })
  @IsOptional()
  @IsIn(["web", "mobile"])
  platform?: PaymentPlatformDto;

  @ApiPropertyOptional({ description: "Payer account / mobile number (e.g. for Waafi MWALLET)" })
  @IsOptional()
  @IsString()
  payerAccount?: string;

  @ApiPropertyOptional({ description: "Browser return URL after successful payment" })
  @IsOptional()
  @IsString()
  returnUrl?: string;

  @ApiPropertyOptional({ description: "Browser return URL after failed/cancelled payment" })
  @IsOptional()
  @IsString()
  failureUrl?: string;
}

export class RefundDto {
  @ApiProperty({ example: "booking-uuid" })
  @IsString()
  bookingId!: string;

  @ApiPropertyOptional({ description: "Optional reason for refund" })
  @IsOptional()
  @IsString()
  reason?: string;
}

export class ClientActionDto {
  @ApiProperty({ enum: ["REDIRECT", "LAUNCH_APP", "COLLECT_OTP"] })
  type!: "REDIRECT" | "LAUNCH_APP" | "COLLECT_OTP";

  @ApiPropertyOptional({ description: "Set when type=REDIRECT (web flow)" })
  url?: string;

  @ApiPropertyOptional({ description: "Set when type=LAUNCH_APP (mobile flow)" })
  appId?: string;

  @ApiPropertyOptional({ description: "Set when type=LAUNCH_APP (mobile flow)" })
  receiveCode?: string;

  @ApiPropertyOptional({ description: "Set when type=LAUNCH_APP (mobile flow)" })
  shortCode?: string;

  @ApiPropertyOptional({ description: "Set when type=COLLECT_OTP (e.g. CAC Bank)" })
  providerOrderId?: string;

  @ApiPropertyOptional({ description: "Set when type=COLLECT_OTP" })
  message?: string;
}

export class InitiateResponseDto {
  @ApiProperty()
  intentId!: string;

  @ApiProperty({ enum: ProviderPaymentStatus })
  status!: ProviderPaymentStatus;

  @ApiPropertyOptional({ type: ClientActionDto })
  clientAction?: ClientActionDto;

  @ApiPropertyOptional()
  merchantOrderId?: string;
}

export class IntentStatusDto extends InitiateResponseDto {
  @ApiPropertyOptional()
  paidAt?: string;

  @ApiPropertyOptional()
  failureCode?: string;

  @ApiPropertyOptional()
  failureMessage?: string;
}
