import type { CardItem, CommentItem, PostItem, Profile, Rarity, TradeItem } from "@/lib/types";

export type ProfileRow = {
  user_id: string;
  handle: string;
  display_name: string;
  bio: string;
  avatar_url: string | null;
  created_at: string | Date;
};

export type CardRow = {
  id: string;
  owner_id: string;
  player_name: string;
  team: string;
  position: string;
  season_year: number;
  rarity: string;
  batting_avg: string | null;
  home_runs: number | null;
  rbi: number | null;
  era: string | null;
  wins: number | null;
  strikeouts: number | null;
  image_data: string | null;
  description: string;
  for_trade: boolean;
  created_at: string | Date;
  owner_handle?: string;
  owner_display_name?: string;
  owner_avatar_url?: string | null;
};

export function mapProfile(row: ProfileRow, extras?: Partial<Profile>): Profile {
  return {
    userId: row.user_id,
    handle: row.handle,
    displayName: row.display_name,
    bio: row.bio,
    avatarUrl: row.avatar_url,
    createdAt: toIso(row.created_at),
    ...extras,
  };
}

export function mapCard(row: CardRow): CardItem {
  return {
    id: row.id,
    ownerId: row.owner_id,
    playerName: row.player_name,
    team: row.team,
    position: row.position,
    seasonYear: Number(row.season_year),
    rarity: row.rarity as Rarity,
    battingAvg: row.batting_avg,
    homeRuns: row.home_runs == null ? null : Number(row.home_runs),
    rbi: row.rbi == null ? null : Number(row.rbi),
    era: row.era,
    wins: row.wins == null ? null : Number(row.wins),
    strikeouts: row.strikeouts == null ? null : Number(row.strikeouts),
    imageData: row.image_data,
    description: row.description,
    forTrade: Boolean(row.for_trade),
    createdAt: toIso(row.created_at),
    ownerHandle: row.owner_handle,
    ownerDisplayName: row.owner_display_name,
    ownerAvatarUrl: row.owner_avatar_url ?? null,
  };
}

export function mapPost(row: {
  id: string;
  user_id: string;
  content: string;
  card_id: string | null;
  created_at: string | Date;
  author_handle: string;
  author_display_name: string;
  author_avatar_url: string | null;
  like_count: number | string;
  comment_count: number | string;
  liked_by_me: boolean | number | string;
}): PostItem {
  return {
    id: row.id,
    userId: row.user_id,
    content: row.content,
    cardId: row.card_id,
    createdAt: toIso(row.created_at),
    authorHandle: row.author_handle,
    authorDisplayName: row.author_display_name,
    authorAvatarUrl: row.author_avatar_url,
    likeCount: Number(row.like_count),
    commentCount: Number(row.comment_count),
    likedByMe: Boolean(row.liked_by_me === true || row.liked_by_me === 1 || row.liked_by_me === "1" || row.liked_by_me === "t"),
  };
}

export function mapComment(row: {
  id: string;
  post_id: string;
  user_id: string;
  content: string;
  created_at: string | Date;
  author_handle: string;
  author_display_name: string;
  author_avatar_url: string | null;
}): CommentItem {
  return {
    id: row.id,
    postId: row.post_id,
    userId: row.user_id,
    content: row.content,
    createdAt: toIso(row.created_at),
    authorHandle: row.author_handle,
    authorDisplayName: row.author_display_name,
    authorAvatarUrl: row.author_avatar_url,
  };
}

export function mapTrade(row: {
  id: string;
  from_user_id: string;
  to_user_id: string;
  offered_card_id: string;
  requested_card_id: string;
  message: string;
  status: string;
  created_at: string | Date;
  resolved_at: string | Date | null;
  from_handle: string;
  from_display_name: string;
  to_handle: string;
  to_display_name: string;
  offered: CardRow;
  requested: CardRow;
}): TradeItem {
  return {
    id: row.id,
    fromUserId: row.from_user_id,
    toUserId: row.to_user_id,
    offeredCardId: row.offered_card_id,
    requestedCardId: row.requested_card_id,
    message: row.message,
    status: row.status as TradeItem["status"],
    createdAt: toIso(row.created_at),
    resolvedAt: row.resolved_at ? toIso(row.resolved_at) : null,
    fromHandle: row.from_handle,
    fromDisplayName: row.from_display_name,
    toHandle: row.to_handle,
    toDisplayName: row.to_display_name,
    offeredCard: mapCard(row.offered),
    requestedCard: mapCard(row.requested),
  };
}

function toIso(value: string | Date): string {
  if (value instanceof Date) return value.toISOString();
  return new Date(value).toISOString();
}
