import {
  ArrayMinSize,
  IsArray,
  IsBoolean,
  IsInt,
  IsOptional,
  IsString,
  Matches,
  MaxLength,
  MinLength,
} from 'class-validator';
import { Type } from 'class-transformer';

import { DocumentStatus } from '../entities/document.entity';
import { IsEnum } from 'class-validator';

/**
 * PATCH /documents/:id — admin edits metadata / security / role assignments.
 * All fields optional. Sent as JSON (no multipart), so booleans stay boolean.
 */
export class UpdateDocumentDto {
  @IsOptional()
  @IsString()
  @MaxLength(200)
  title?: string;

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

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

  // If present → replaces the whole role set for this doc.
  // If omitted → role assignments are left untouched.
  @IsOptional()
  @IsArray()
  @ArrayMinSize(1)
  @IsInt({ each: true })
  @Type(() => Number)
  role_ids?: number[];

  @IsOptional()
  @IsBoolean()
  require_otp?: boolean;

  @IsOptional()
  @IsBoolean()
  allow_download?: boolean;

  // Non-empty string → new password (hashed), empty string → clear password.
  @IsOptional()
  @IsString()
  @MinLength(0)
  @MaxLength(120)
  password?: string;

  @IsOptional()
  @IsString()
  @Matches(/^\d{4}-\d{2}-\d{2}$/, {
    message: 'expiry_date must be YYYY-MM-DD',
  })
  expiry_date?: string | null;

  @IsOptional()
  @IsEnum(DocumentStatus)
  status?: DocumentStatus;
}
