import { ApiExtraModels, ApiProperty, getSchemaPath } from '@nestjs/swagger';
import {
  IsArray,
  IsBoolean,
  IsNumber,
  IsObject,
  IsOptional,
  IsString,
  ValidateNested,
} from 'class-validator';
import { AssetQueryDTOWhere } from './asset-query-dto';
import { Type } from 'class-transformer';
import { AssetPropValue } from '../logic/Props';

export class AssetBlockParamsDTO {
  @IsOptional()
  @ApiProperty({})
  name?: string | null;

  @IsOptional()
  @ApiProperty({})
  title?: string | null;

  @IsNumber()
  @IsOptional()
  @ApiProperty({})
  index?: number;

  @IsBoolean()
  @IsOptional()
  @ApiProperty({
    default: false,
  })
  delete?: boolean;
}

export class AssetPropsParamsDTO {
  [x: string]: AssetPropValue;
}

export class AssetMainParams {
  @IsArray()
  @IsOptional()
  @ApiProperty({})
  parentIds?: string[];

  @IsString()
  @IsOptional()
  @ApiProperty({})
  name?: string;

  @IsString()
  @IsOptional()
  @ApiProperty({})
  title?: string;

  @IsString()
  @IsOptional()
  @ApiProperty({})
  icon?: string;

  @IsString()
  @IsOptional()
  @ApiProperty({})
  workspaceId?: string;

  @IsBoolean()
  @IsOptional()
  @ApiProperty({ default: false })
  isAbstract?: boolean;

  @IsString()
  @IsOptional()
  @ApiProperty({})
  scope?: string;

  @IsNumber()
  @IsOptional()
  @ApiProperty({})
  index?: number;

  @IsNumber()
  @IsOptional()
  @ApiProperty({})
  creatorUserId?: number;
}

@ApiExtraModels(AssetBlockParamsDTO)
@ApiExtraModels(AssetPropsParamsDTO)
export class AssetCreateDTO {
  @ApiProperty({ type: AssetMainParams })
  @ValidateNested()
  @Type(() => AssetMainParams)
  main?: AssetMainParams;

  @ApiProperty({
    type: 'object',
    additionalProperties: { $ref: getSchemaPath(AssetBlockParamsDTO) },
  })
  blocks?: {
    [blockRef: string]: AssetBlockParamsDTO;
  };

  @ApiProperty({
    type: 'object',
    additionalProperties: { $ref: getSchemaPath(AssetPropsParamsDTO) },
  })
  props?: {
    [blockRef: string]: AssetPropsParamsDTO | AssetPropsParamsDTO[];
  };
}

export class AssetChangeDTO extends AssetCreateDTO {
  @ApiProperty({ type: () => AssetQueryDTOWhere })
  @IsObject()
  where: AssetQueryDTOWhere;
}

export class AssetDeleteResultDTO {
  @ApiProperty()
  ids: string[];
}
