import { ApiPropertyOptional, ApiProperty } from "@nestjs/swagger";
import { Type } from "class-transformer";
import {
  IsArray,
  IsBoolean,
  IsInt,
  IsOptional,
  IsString,
  MaxLength,
  Min,
  MinLength,
  ValidateNested,
} from "class-validator";

export class UpdateContractTemplateDto {
  @ApiPropertyOptional({ description: "Display name of the template" })
  @IsOptional()
  @IsString()
  @MinLength(3)
  @MaxLength(200)
  name?: string;

  @ApiPropertyOptional({ description: "Short description shown on the template card" })
  @IsOptional()
  @IsString()
  description?: string;

  @ApiPropertyOptional({ description: "Cover-page service title of the generated document" })
  @IsOptional()
  @IsString()
  @MinLength(3)
  @MaxLength(300)
  documentTitle?: string;

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

  @ApiPropertyOptional({ description: "Whether the template is used for generation" })
  @IsOptional()
  @IsBoolean()
  isActive?: boolean;
}

export class CreateArticleDto {
  @ApiProperty({ description: "Article heading (without the Article N prefix)" })
  @IsString()
  @MinLength(2)
  @MaxLength(200)
  title!: string;

  @ApiProperty({
    description:
      'Article body. One clause per line; prefix a line with "- " to nest it as a bullet under the previous clause.',
  })
  @IsString()
  @MinLength(2)
  body!: string;

  @ApiPropertyOptional({ description: "1-based position to insert at (appends when omitted)" })
  @IsOptional()
  @IsInt()
  @Min(1)
  position?: number;
}

export class UpdateArticleDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MinLength(2)
  @MaxLength(200)
  title?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MinLength(2)
  body?: string;
}

export class ReplaceArticleDto {
  @ApiPropertyOptional({ description: "Existing article id (new id assigned when omitted)" })
  @IsOptional()
  @IsString()
  id?: string;

  @ApiProperty()
  @IsString()
  @MinLength(2)
  @MaxLength(200)
  title!: string;

  @ApiProperty()
  @IsString()
  @MinLength(2)
  body!: string;
}

export class ReplaceArticlesDto {
  @ApiProperty({ type: [ReplaceArticleDto], description: "Full ordered article list" })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ReplaceArticleDto)
  articles!: ReplaceArticleDto[];
}

/** Optional draft overrides so the editor can preview unsaved changes. */
export class PreviewContractTemplateDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  name?: string;

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

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

  @ApiPropertyOptional({ type: [ReplaceArticleDto] })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ReplaceArticleDto)
  articles?: ReplaceArticleDto[];
}
