import {
  IsString,
  IsOptional,
  IsBoolean,
  IsNumber,
  IsArray,
  IsObject,
  IsUUID,
  IsUrl,
  MaxLength,
  MinLength,
  Min,
  Max,
} from 'class-validator';

// ============================================
// ENDPOINT DTOs
// ============================================

export class CreateWebhookEndpointDto {
  @IsString()
  @MinLength(2)
  @MaxLength(100)
  name: string;

  @IsOptional()
  @IsString()
  description?: string;

  @IsUrl()
  @MaxLength(500)
  url: string;

  @IsOptional()
  @IsString()
  @MaxLength(20)
  authType?: string;

  @IsOptional()
  @IsObject()
  authConfig?: Record<string, any>;

  @IsArray()
  @IsString({ each: true })
  subscribedEvents: string[];

  @IsOptional()
  @IsObject()
  headers?: Record<string, string>;

  @IsOptional()
  @IsNumber()
  @Min(1000)
  @Max(30000)
  timeout?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  @Max(10)
  retryCount?: number;

  @IsOptional()
  @IsBoolean()
  isActive?: boolean;
}

export class UpdateWebhookEndpointDto {
  @IsOptional()
  @IsString()
  @MaxLength(100)
  name?: string;

  @IsOptional()
  @IsString()
  description?: string;

  @IsOptional()
  @IsUrl()
  @MaxLength(500)
  url?: string;

  @IsOptional()
  @IsString()
  @MaxLength(20)
  authType?: string;

  @IsOptional()
  @IsObject()
  authConfig?: Record<string, any>;

  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  subscribedEvents?: string[];

  @IsOptional()
  @IsObject()
  headers?: Record<string, string>;

  @IsOptional()
  @IsNumber()
  @Min(1000)
  @Max(30000)
  timeout?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  @Max(10)
  retryCount?: number;
}

export class ToggleWebhookEndpointDto {
  @IsBoolean()
  isActive: boolean;
}

// ============================================
// EVENT DTOs
// ============================================

export class CreateWebhookEventDto {
  @IsString()
  @MaxLength(100)
  eventType: string;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  entityType?: string;

  @IsOptional()
  @IsUUID()
  entityId?: string;

  @IsObject()
  payload: Record<string, any>;

  @IsOptional()
  @IsObject()
  metadata?: Record<string, any>;
}

// ============================================
// DELIVERY DTOs
// ============================================

export class UpdateDeliveryResultDto {
  @IsString()
  @MaxLength(20)
  status: string;

  @IsOptional()
  @IsNumber()
  responseStatus?: number;

  @IsOptional()
  @IsString()
  responseBody?: string;

  @IsOptional()
  @IsNumber()
  durationMs?: number;

  @IsOptional()
  @IsString()
  errorMessage?: string;
}

export class ScheduleRetryDto {
  @IsString()
  nextRetryAt: string;

  @IsNumber()
  @Min(1)
  attemptNumber: number;
}
