import {
  IsString,
  IsUUID,
  IsOptional,
  IsObject,
  ValidateNested,
  MinLength,
  MaxLength,
  IsEnum,
} from "class-validator";
import { Type } from "class-transformer";

/**
 * Valid webhook action types
 */
export enum WebhookAction {
  ASSIGNED = "assigned",
  MENTIONED = "mentioned",
  COMMENTED = "commented",
}

/**
 * DTO for webhook payload from @mosaic bot
 */
export class WebhookPayloadDto {
  @IsString({ message: "issueNumber must be a string" })
  @MinLength(1, { message: "issueNumber must not be empty" })
  @MaxLength(50, { message: "issueNumber must not exceed 50 characters" })
  issueNumber!: string;

  @IsString({ message: "repository must be a string" })
  @MinLength(1, { message: "repository must not be empty" })
  @MaxLength(512, { message: "repository must not exceed 512 characters" })
  repository!: string;

  @IsEnum(WebhookAction, { message: "action must be one of: assigned, mentioned, commented" })
  action!: WebhookAction;

  @IsOptional()
  @IsString({ message: "comment must be a string" })
  @MaxLength(10000, { message: "comment must not exceed 10000 characters" })
  comment?: string;

  @IsOptional()
  @IsObject({ message: "metadata must be an object" })
  metadata?: Record<string, unknown>;
}

/**
 * DTO for dispatching a job
 */
export class DispatchJobDto {
  @IsUUID("4", { message: "workspaceId must be a valid UUID v4" })
  workspaceId!: string;

  @IsString({ message: "type must be a string" })
  @MinLength(1, { message: "type must not be empty" })
  @MaxLength(100, { message: "type must not exceed 100 characters" })
  type!: string; // 'git-status', 'code-task', 'priority-calc'

  @IsOptional()
  @ValidateNested({ message: "webhookPayload must be a valid WebhookPayloadDto" })
  @Type(() => WebhookPayloadDto)
  webhookPayload?: WebhookPayloadDto;

  @IsOptional()
  @IsObject({ message: "context must be an object" })
  context?: Record<string, unknown>;
}
