import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsIn, IsOptional, IsString } from 'class-validator';
import { PaginationQueryDto } from '../../../common/dto/pagination-query.dto';
import { PostType } from '../../../common/enums/post-type.enum';
import { PostVisibility } from '../../../common/enums/post-visibility.enum';

export const POST_SORT_FIELDS = [
  'createdAt',
  'updatedAt',
  'likesCount',
  'commentsCount',
  'savesCount',
  'shareCount',
  'viewCount',
  'playCount',
] as const;

export type PostSortField = (typeof POST_SORT_FIELDS)[number];
export const POST_VISIBILITY_FILTERS = [...Object.values(PostVisibility), 'archived'] as const;
export type PostVisibilityFilter = (typeof POST_VISIBILITY_FILTERS)[number];
export const POST_MEDIA_TYPE_FILTERS = [...Object.values(PostType), 'reel'] as const;
export type PostMediaTypeFilter = (typeof POST_MEDIA_TYPE_FILTERS)[number];

export class PostQueryDto extends PaginationQueryDto {
  @ApiPropertyOptional({ enum: POST_VISIBILITY_FILTERS })
  @IsOptional()
  @IsIn(POST_VISIBILITY_FILTERS)
  visibility?: PostVisibilityFilter;

  @ApiPropertyOptional({ enum: POST_MEDIA_TYPE_FILTERS })
  @IsOptional()
  @IsIn(POST_MEDIA_TYPE_FILTERS)
  postType?: PostMediaTypeFilter;

  @ApiPropertyOptional({ enum: POST_MEDIA_TYPE_FILTERS })
  @IsOptional()
  @IsIn(POST_MEDIA_TYPE_FILTERS)
  mediaType?: PostMediaTypeFilter;

  @ApiPropertyOptional({ description: 'Search inside post content' })
  @IsOptional()
  @IsString()
  q?: string;

  @ApiPropertyOptional({ description: 'Filter by hashtag without the # prefix', example: 'music' })
  @IsOptional()
  @IsString()
  hashtag?: string;

  @ApiPropertyOptional({ enum: POST_SORT_FIELDS, default: 'createdAt' })
  @IsOptional()
  @IsIn(POST_SORT_FIELDS)
  sortBy?: PostSortField;
}
