import {
  ApiExtraModels,
  ApiHideProperty,
  ApiProperty,
  ApiPropertyOptional,
  IntersectionType,
  OmitType,
  PartialType,
  PickType,
} from '@nestjs/swagger';
import { Expose, Transform, Type } from 'class-transformer';
import { IsArray, IsEnum, IsInt, IsOptional, IsString } from 'class-validator';
import dayjs from 'dayjs';
import { CountDto, DateSearchDto, PaginationDto } from '../common/common.dto';
import { Member } from '../fc/fc.entity';
import { Alarm } from './alarm.entity';

export class AlarmListDto extends IntersectionType(
  PaginationDto,
  DateSearchDto,
  PartialType(PickType(Member, ['memId'])),
) {
  @ApiHideProperty()
  dateType = 'almCreatedAt';

  @ApiHideProperty()
  @Transform(() =>
    dayjs().subtract(1, 'month').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
  )
  startDate = dayjs()
    .subtract(1, 'month')
    .startOf('day')
    .format('YYYY-MM-DD HH:mm:ss');

  @ApiHideProperty()
  endDate = dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss');

  @ApiPropertyOptional({
    description: '알림 타입 필터 (SND001: 결제, SND002: 분석, SND003: 팩스, SND004: 이벤트)',
    enum: ['SND001', 'SND002', 'SND003', 'SND004'],
  })
  @IsOptional()
  @IsString()
  almType?: string;
}

export class AlarmCheckDto extends PickType(Alarm, ['almId']) {}

export class AlarmCreatedDto extends IntersectionType(
  PickType(Alarm, ['almUserName', 'almType', 'almTitle', 'almContent', 'almMessage']),
  PickType(Member, ['memId']),
) {}

//--------------------- ResponseDto ---------------------//
class AlarmDto extends OmitType(Alarm, [
  'almType',
  'almUpdatedAt',
  'almChecked',
]) {
  @Expose()
  @ApiProperty({ description: '알람타입', enum: ['결제', '분석', '팩스', '이벤트'] })
  @IsEnum(['결제', '분석', '팩스', '이벤트'])
  almType!: '결제' | '분석' | '팩스' | '이벤트';

  @Expose()
  @ApiProperty({ description: '읽음여부', enum: ['읽음', '읽지않음'] })
  @IsEnum(['읽음', '읽지않음'])
  almChecked!: '읽음' | '읽지않음';

  @Expose()
  @ApiPropertyOptional({ description: '리다이렉트 URL' })
  @IsOptional()
  almRedirectUrl?: string;

  @Expose()
  @ApiPropertyOptional({ description: '푸시 ID' })
  @IsOptional()
  pshId?: number;
}

@ApiExtraModels(AlarmDto, CountDto)
export class AlarmListResponseDto extends IntersectionType(CountDto) {
  @Expose()
  @IsInt()
  @ApiProperty({ description: '읽지않은 알람수' })
  noCheckCount!: number;

  @Expose()
  @ApiPropertyOptional({ type: [AlarmDto], description: '목록' })
  @Type(() => AlarmDto)
  @IsArray()
  @IsOptional()
  list?: AlarmDto[];
}
