import {
  IsBoolean,
  IsIn,
  IsOptional,
  IsString,
  MaxLength,
  ValidateIf,
} from 'class-validator';

/**
 * Payload for creating a communication template. `channel` and
 * `context` are frozen once created (enforced at the service layer)
 * because changing either would invalidate placeholders in the body.
 * Rename or edit body freely via the update DTO.
 *
 * `subject` is required for EMAIL rows and MUST be null for WHATSAPP
 * rows — mirrors the DB CHECK constraint.
 */
export class CreateCommunicationTemplateDto {
  @IsIn(['EMAIL', 'WHATSAPP'])
  channel: 'EMAIL' | 'WHATSAPP';

  @IsIn(['QUERY', 'BOOKING', 'MOVEMENT_CUSTOMER', 'MOVEMENT_SUPPLIER'])
  context: 'QUERY' | 'BOOKING' | 'MOVEMENT_CUSTOMER' | 'MOVEMENT_SUPPLIER';

  @IsString() @MaxLength(120)
  name: string;

  // Required when channel = EMAIL; optional/null otherwise.
  @ValidateIf((o) => o.channel === 'EMAIL')
  @IsString() @MaxLength(200)
  subject?: string | null;

  @IsString()
  body_template: string;

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

  @IsBoolean() @IsOptional()
  is_active?: boolean;
}
