import { applyDecorators } from '@nestjs/common';
import { Type } from 'class-transformer';
import {
  ArrayMaxSize,
  IsArray,
  IsBoolean,
  IsNotEmpty,
  IsOptional,
  IsString,
  MaxLength,
  ValidateNested,
} from 'class-validator';

/**
 * One photo / video on a create or update payload. The URL must be one
 * returned by `POST /uploads` for the same module; type and filename
 * are derived server-side from the stored key.
 */
export class MediaItemDto {
  @IsString()
  @IsNotEmpty()
  @MaxLength(500)
  url: string;

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

/**
 * `media` property on module DTOs. Omitted → existing media untouched;
 * `[]` → remove all. Per-module limits are enforced by MediaService.
 */
export function MediaListField(): PropertyDecorator {
  return applyDecorators(
    IsOptional(),
    IsArray(),
    ArrayMaxSize(50),
    ValidateNested({ each: true }),
    Type(() => MediaItemDto),
  );
}
