import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
  ArrayMinSize,
  IsArray,
  IsDateString,
  IsInt,
  ValidateIf,
  ValidateNested,
} from 'class-validator';

/** Set (or clear, via null) a session's bulk link-generation time. */
export class SetLinkGenerationDto {
  @ApiProperty({
    description:
      'Future ISO 8601 (UTC) time to generate links; explicit null clears/cancels. ' +
      'Required — omitting it is a validation error, never a silent cancel.',
    example: '2026-07-10T09:30:00Z',
    nullable: true,
  })
  @ValidateIf((o) => o.linkGenerationAt !== null)
  @IsDateString()
  linkGenerationAt: string | null;
}

/** One session's target link-generation time within a bulk request. */
export class SetLinkGenerationItemDto {
  @ApiProperty({
    description: 'Program session to (re)schedule link generation for.',
    example: 42,
  })
  @IsInt()
  programSessionId!: number;

  @ApiProperty({
    description: 'Future ISO 8601 (UTC) time to generate links; explicit null clears/cancels.',
    example: '2026-07-10T09:30:00Z',
    nullable: true,
  })
  @ValidateIf((o) => o.linkGenerationAt !== null)
  @IsDateString()
  linkGenerationAt: string | null;
}

/**
 * Bulk payload: set (or clear) the link-generation time for several program
 * sessions in one request, each with respect to its own `linkGenerationAt`.
 */
export class SetLinkGenerationBulkDto {
  @ApiProperty({
    description: 'Sessions to (re)schedule, one entry per program session.',
    type: [SetLinkGenerationItemDto],
  })
  @IsArray()
  @ArrayMinSize(1)
  @ValidateNested({ each: true })
  @Type(() => SetLinkGenerationItemDto)
  sessions!: SetLinkGenerationItemDto[];
}
