import {
  ApiProperty,
  ApiPropertyOptional,
  IntersectionType,
  OmitType,
} from '@nestjs/swagger';
import { Expose, Type } from 'class-transformer';
import { IsArray, IsObject, IsOptional } from 'class-validator';

import {
  CountDto,
  DateSearchDto,
  IdsDto,
  PaginationDto,
  SearchDto,
} from '../common/common.dto';
import { Authority } from './authority.entity';

export class GetAuthorityListDto extends IntersectionType(
  PaginationDto,
  DateSearchDto,
  SearchDto,
) {
  @ApiPropertyOptional({
    description: '검색날짜 타입 (default: athCreatedAt)',
    enum: ['athCreatedAt'],
  })
  dateType = 'athCreatedAt';

  @ApiPropertyOptional({
    description: '검색조건 (default: athName)',
    enum: ['athName'],
  })
  searchKey = 'athName';

  @ApiPropertyOptional({
    enum: ['athName', 'athCreatedAt'],
  })
  sortKey = 'athCreatedAt';
}

export class CreateAuthorityDto extends OmitType(Authority, [
  'athId',
  'athCreatedAt',
  'athUpdatedAt',
]) {
  @Expose()
  @ApiProperty({ description: '메뉴 코드 목록' })
  @IsArray()
  codes!: string[];
}

export class UpdateAuthorityDto extends OmitType(Authority, [
  'athId',
  'athCreatedAt',
  'athUpdatedAt',
]) {
  @Expose()
  @ApiProperty({ description: '메뉴 코드 목록' })
  @IsArray()
  codes!: string[];
}

export class DeleteAuthorityDto extends IntersectionType(IdsDto) {}

//--------------------- ResponseDto ---------------------//
export class GetAuthorityDto extends OmitType(Authority, []) {}

export class GetAuthorityListResponseDto extends IntersectionType(CountDto) {
  @Expose()
  @ApiPropertyOptional({ description: '목록' })
  @Type(() => GetAuthorityDto)
  @IsArray()
  @IsOptional()
  list?: GetAuthorityDto[];
}

export class GetAuthorityResponseDto {
  @Expose()
  @ApiProperty({
    type: [GetAuthorityDto],
    description: '권한 상세',
  })
  @Type(() => GetAuthorityDto)
  @IsObject()
  detail!: GetAuthorityDto;

  @Expose()
  @ApiProperty({
    description: '메뉴 코드 배열',
  })
  @IsArray()
  codes!: string[];
}

export class GetAuthMenusResponseDto {
  @Expose()
  @ApiProperty({
    description: '권한 메뉴 코드 목록',
  })
  @IsArray()
  codes!: string[];
}
