import { Type } from 'class-transformer';
import {
  IsDefined,
  IsEnum,
  IsNotEmpty,
  IsOptional,
  IsString,
  ValidateNested,
} from 'class-validator';

export enum IntegrationType {
  WECOM = 'WECOM',
  DINGTALK = 'DINGTALK',
}

export class DingTalkThirdConfigDto {
  @IsString()
  @IsNotEmpty()
  agentId: string;

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

  @IsString()
  @IsNotEmpty()
  corpId: string;

  @IsOptional()
  @IsString()
  appId?: string;
}

export class WeComThirdConfigDto {
  @IsString()
  @IsNotEmpty()
  agentId: string;

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

  @IsString()
  @IsNotEmpty()
  corpId: string;
}

export class IntegrationConfigRequestDto {
  @IsEnum(IntegrationType)
  type: IntegrationType;

  @IsDefined()
  @ValidateNested()
  @Type((options) =>
    options?.object?.type === IntegrationType.DINGTALK
      ? DingTalkThirdConfigDto
      : WeComThirdConfigDto,
  )
  config: DingTalkThirdConfigDto | WeComThirdConfigDto;
}

export class SaveIntegrationConfigDto extends IntegrationConfigRequestDto {}

export class TestIntegrationConfigDto extends IntegrationConfigRequestDto {}

/** 对外返回的配置（脱敏后，不含 appSecret） */
export interface ThirdConfigBaseDTO<T = unknown> {
  type: string;
  verify?: boolean;
  config: T;
}
