/**
 * PostCard 组件类型定义
 * 统一的帖子卡片组件类型系统
 */

import { StyleProp, ViewStyle } from 'react-native';
import { Post, PostImageDTO, UserDTO, CommentDTO } from '../../../types';
import { ImageGridItem } from '../../common';

// ==================== Action 类型定义 ====================

/**
 * PostCard 支持的操作类型
 */
export type PostCardActionType =
  | 'press' // 点击帖子主体
  | 'userPress' // 点击用户头像/名称
  | 'like' // 点赞
  | 'unlike' // 取消点赞
  | 'comment' // 评论
  | 'bookmark' // 收藏
  | 'unbookmark' // 取消收藏
  | 'share' // 分享
  | 'imagePress' // 点击图片
  | 'delete'; // 删除

/**
 * Action payload 类型
 */
export interface PostCardActionPayload {
  images?: ImageGridItem[];
  imageIndex?: number;
  [key: string]: unknown;
}

/**
 * 统一的 Action 对象
 */
export interface PostCardAction {
  type: PostCardActionType;
  payload?: PostCardActionPayload;
}

/**
 * 统一的回调函数类型
 */
export type PostCardOnAction = (action: PostCardAction) => void;

// ==================== Features 配置类型 ====================

/**
 * 显示特性配置
 */
export interface PostCardFeatures {
  /** 显示用户信息 */
  showAuthor?: boolean;
  /** 显示标题 */
  showTitle?: boolean;
  /** 显示内容 */
  showContent?: boolean;
  /** 显示图片 */
  showImages?: boolean;
  /** 显示投票预览 */
  showVote?: boolean;
  /** 显示热门评论 */
  showTopComment?: boolean;
  /** 显示操作栏 */
  showActions?: boolean;
  /** 显示浏览数 */
  showViews?: boolean;
  /** 显示分享按钮 */
  showShare?: boolean;
  /** 显示收藏按钮 */
  showBookmark?: boolean;
  /** 显示删除按钮（需配合权限判断） */
  showDelete?: boolean;
  /** 显示置顶标签 */
  showPinned?: boolean;
  /** 显示徽章（楼主/管理员） */
  showBadges?: boolean;
}

/**
 * Features 预设名称
 */
export type PostCardFeaturesPreset = 'full' | 'compact' | 'grid';

/**
 * Features 配置（可以是预设名称或自定义对象）
 */
export type PostCardFeaturesConfig = PostCardFeaturesPreset | PostCardFeatures;

// ==================== 主组件 Props ====================

/**
 * PostCard 主组件 Props
 */
export interface PostCardProps {
  /** 帖子数据 */
  post: Post;

  /** 统一事件回调 */
  onAction: PostCardOnAction;

  /** 显示模式：列表模式 | 网格模式 */
  variant?: 'list' | 'grid';

  /** 预设配置名称，或自定义配置对象 */
  features?: PostCardFeaturesConfig;

  /** 当前用户是否为帖子作者（用于显示删除按钮） */
  isPostAuthor?: boolean;

  /** 当前用户是否为楼主（在帖子详情页显示"楼主"徽章） */
  isAuthor?: boolean;

  /** 索引（用于虚拟化列表优化） */
  index?: number;

  /** 自定义样式 */
  style?: StyleProp<ViewStyle>;
}

// ==================== 子组件 Props ====================

/**
 * PostHeader 子组件 Props
 */
export interface PostHeaderProps {
  author: UserDTO;
  createdAt: string;
  editedAt?: string;
  isPinned?: boolean;
  badges?: Array<{ type: 'author' | 'admin'; label: string }>;
  onDelete?: () => void;
  onUserPress: () => void;
  showDelete?: boolean;
  showPinned?: boolean;
  showBadges?: boolean;
  isDeleting?: boolean;
}

/**
 * PostTitle 子组件 Props
 */
export interface PostTitleProps {
  title?: string;
  numberOfLines?: number;
}

/**
 * PostContent 子组件 Props
 */
export interface PostContentProps {
  content?: string;
  compact?: boolean;
  maxLines?: number;
  isExpanded?: boolean;
  onToggleExpand?: () => void;
}

/**
 * PostImages 子组件 Props
 */
export interface PostImagesProps {
  images: PostImageDTO[];
  displayMode: 'list' | 'grid';
  onImagePress: (images: ImageGridItem[], index: number) => void;
}

/**
 * PostActions 子组件 Props
 */
export interface PostActionsProps {
  viewsCount: number;
  likesCount: number;
  commentsCount: number;
  isLiked: boolean;
  isFavorited: boolean;
  onLike: () => void;
  onComment: () => void;
  onShare: () => void;
  onBookmark: () => void;
  showViews?: boolean;
  showShare?: boolean;
  showBookmark?: boolean;
}

/**
 * PostVotePreview 子组件 Props
 */
export interface PostVotePreviewProps {
  isVote: boolean;
  totalVotes?: number;
  optionsCount?: number;
  onPress: () => void;
}

/**
 * PostTopComment 子组件 Props
 */
export interface PostTopCommentProps {
  comment: CommentDTO | null;
  totalComments: number;
  onPress: () => void;
}

/**
 * PostGridCover 子组件 Props
 */
export interface PostGridCoverProps {
  image: PostImageDTO | null;
  content?: string;
  isVote?: boolean;
  aspectRatio: number;
  onImagePress: () => void;
}

/**
 * PostGridInfo 子组件 Props
 */
export interface PostGridInfoProps {
  title?: string;
  author: UserDTO;
  likesCount: number;
  onUserPress: () => void;
}

// ==================== 容器组件 Props ====================

/**
 * PostCardList 容器组件 Props
 */
export interface PostCardListProps {
  post: Post;
  onAction: PostCardOnAction;
  features: PostCardFeatures;
  isPostAuthor?: boolean;
  isAuthor?: boolean;
  index?: number;
  style?: StyleProp<ViewStyle>;
}

/**
 * PostCardGrid 容器组件 Props
 */
export interface PostCardGridProps {
  post: Post;
  onAction: PostCardOnAction;
  features: PostCardFeatures;
  style?: StyleProp<ViewStyle>;
}

// ==================== 工具类型 ====================

/**
 * 徽章类型
 */
export interface Badge {
  type: 'author' | 'admin';
  label: string;
}
