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

export class ReturnReportDto {
  @IsString() @MinLength(3) @MaxLength(2000)
  reason: string;
}

export class RaiseNcDto {
  /** Major | Minor | Observation (same values the NC module uses). */
  @IsEnum(['Major', 'Minor', 'Observation'])
  nc_type: 'Major' | 'Minor' | 'Observation';

  /** Statement of the finding. If omitted, the clause evidence text is used. */
  @IsOptional() @IsString() @MaxLength(20000)
  statement?: string;

  @IsOptional() @IsString() @MaxLength(5000)
  corrective_action?: string;
}

export class SeedLibraryDto {
  /** standards.id the clause set is seeded under (4 = ISO 9001:2015, 33 = ISO 14001:2026 in scheme_dbs). */
  @IsInt() @Type(() => Number)
  standard_id: number;

  /** true = replace the stored original evidence wording with the seed's version (use after the wording was updated). */
  @IsOptional() @IsBoolean()
  overwrite_evidence?: boolean;
}

export class LibraryRowDto {
  @IsEnum(['clause', 'performance', 'risk', 'feedback', 'statutory'])
  group_key: 'clause' | 'performance' | 'risk' | 'feedback' | 'statutory';

  /** Unique per standard + scope + group (e.g. "4.1", "A.5.1"). */
  @IsString() @MaxLength(40) code: string;

  /** Heading the row is printed under, e.g. "Clause 4. Context of the Organization". */
  @IsString() @MaxLength(200) heading: string;

  /** Short label shown in the first column (optional for clauses, used for Stage 2 effectiveness rows). */
  @IsOptional() @IsString() @MaxLength(255) title?: string;

  @IsOptional() @IsString() requirement?: string;

  /** Text pre-filled into the evidence box when a report is created. */
  @IsOptional() @IsString() default_evidence?: string;
}

export class ImportLibraryDto {
  @IsInt() @Type(() => Number) standard_id: number;

  /** readiness = Stage 1 list, full = Stage 2 / Surveillance / Recertification list. Default full. */
  @IsOptional() @IsEnum(['readiness', 'full']) scope?: 'readiness' | 'full';

  @IsArray() @ValidateNested({ each: true }) @Type(() => LibraryRowDto)
  rows: LibraryRowDto[];
}

export class CloneLibraryDto {
  @IsInt() @Type(() => Number) from_standard_id: number;
  @IsInt() @Type(() => Number) to_standard_id: number;
}
