import React, {memo} from 'react';
import {View, StyleProp, ViewStyle} from 'react-native';
import PostPoll from '@components/Post/components/PostBody/components/PostPoll';
import {
  checkHasPoll,
  getLinkPreview,
} from '@components/Post/components/PostBody/utils';
import {DraftUser} from '@interfaces/user';
import {Post as PostType} from '@modules/post/interfaces';
import {ScreenKey} from '@shared/stores/useActivePostsStore';
import {useTheme} from '@shared/theme';

import {PostAttachments} from './components/PostAttachments';
import {PostBody} from './components/PostBody';
import {PostHeader, PostHeaderProps} from './components/PostHeader';
import {styles} from './styles';
import LinkPreviewCard from '../LinkPreviewCard';
import {SharedPost} from './components/SharedPost';

interface PostProps extends PostHeaderProps {
  onPressPost?: () => void;
  style?: StyleProp<ViewStyle>;
  creatorUser?: DraftUser;
  renderDescription?: () => React.ReactNode;
  truncate?: boolean;
  canInteract?: boolean;
  groupId?: number;
  screenKey?: Nullable<ScreenKey>;
  TitleComponent?: React.ReactNode;
  TitleRightComponent?: React.ReactNode;
  HeaderComponent?: React.ReactNode;
  isPreviewPost?: boolean;
  children?: React.ReactNode;
}

function PostComponent({
  onPressPost,
  onPressOptions,
  post,
  style,
  withOptions,
  creatorUser,
  renderDescription,
  truncate = false,
  canInteract = false,
  groupId,
  screenKey,
  TitleComponent,
  TitleRightComponent,
  HeaderComponent,
  isPreviewPost,
  children,
  isGroupPostInFeed,
  canManagePosts,
  isInsightsQuickLinkEnabled,
}: PostProps) {
  const {theme} = useTheme();
  const hasPoll = checkHasPoll(post.poll);
  const linkPreview = getLinkPreview(post.linkPreviews);

  const groupIdWithFallback = groupId || post.group?.id; // Fallback to post's groupId if not provided

  return (
    <View
      style={[
        styles.container,
        {backgroundColor: theme.background.layout.tertiary},
        style,
        (post as PostType).hasBeenSegmented &&
          !isPreviewPost && {
            ...styles.withBorder,
            borderColor: theme.border.neutral.brand,
          },
      ]}>
      <View>
        {HeaderComponent || (
          <PostHeader
            groupId={groupId}
            renderDescription={renderDescription}
            post={post}
            onPressOptions={onPressOptions}
            withOptions={withOptions}
            canManagePosts={canManagePosts}
            creatorUser={creatorUser}
            TitleComponent={TitleComponent}
            TitleRightComponent={TitleRightComponent}
            isPreviewPost={isPreviewPost}
            isGroupPostInFeed={isGroupPostInFeed}
            isInsightsQuickLinkEnabled={isInsightsQuickLinkEnabled}
          />
        )}
        {!!(post.bodyHtml || post.body) && (
          <PostBody onPressBody={onPressPost} post={post} truncate={truncate} />
        )}
      </View>
      {!!post.metadata && !isPreviewPost && (
        <SharedPost metadata={post.metadata} />
      )}
      {!!linkPreview && <LinkPreviewCard data={linkPreview} />}
      {hasPoll && (
        <PostPoll
          canInteract={canInteract}
          groupId={groupIdWithFallback}
          poll={post.poll!}
          postId={post.id}
        />
      )}
      <PostAttachments
        post={post}
        groupId={groupIdWithFallback}
        screenKey={screenKey}
      />
      {children}
    </View>
  );
}

export const Post = memo(PostComponent);

export * from './components';
export * from './interfaces';
