import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
  IsArray,
  IsInt,
  IsOptional,
  IsString,
  ValidateNested,
} from 'class-validator';

/** One article of a per-contract document override sent from the editor. */
export class ContractDocumentArticleDto {
  @ApiPropertyOptional({ description: 'Stable id; omitted for a new article.' })
  @IsOptional()
  @IsString()
  id?: string;

  @ApiProperty()
  @IsString()
  title!: string;

  @ApiProperty({ description: 'Plain multiline body; each line becomes a clause.' })
  @IsString()
  body!: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  order?: number;
}

/**
 * The per-contract document override sent from the accept/edit editor. It edits
 * ONLY this contract's frozen snapshot — it is never written back to the shared
 * six {@link ContractTemplate} rows.
 */
export class UpdateContractDocumentDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  code?: string | null;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  name?: string | null;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  documentTitle?: string | null;

  @ApiPropertyOptional({ type: [String] })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  whereasClauses?: string[];

  @ApiProperty({ type: [ContractDocumentArticleDto] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ContractDocumentArticleDto)
  articles!: ContractDocumentArticleDto[];
}
