import { placeholderSrc } from "@/lib/placeholder";

export type ProjectType = "video" | "image" | "trimmer";

export type ProjectStatus = "draft" | "rendering" | "ready";

export interface DashboardProject {
  id: string;
  name: string;
  type: ProjectType;
  status: ProjectStatus;
  renderUrl: string | null;
  thumbnailSeed: string;
  lastEditedAt: string;
}

export interface ProjectRow {
  id: string;
  user_id: string;
  name: string;
  type: ProjectType;
  scene_data: Record<string, unknown>;
  render_url: string | null;
  status: ProjectStatus;
  created_at: string;
  updated_at: string;
}

export function mapProjectRow(row: ProjectRow): DashboardProject {
  return {
    id: row.id,
    name: row.name,
    type: row.type,
    status: row.status,
    renderUrl: row.render_url,
    thumbnailSeed: row.id.replace(/-/g, "").slice(0, 12),
    lastEditedAt: row.updated_at,
  };
}

export function getProjectStudioPath(project: Pick<DashboardProject, "id" | "type">): string {
  switch (project.type) {
    case "video":
      return `/studio/video/${project.id}`;
    case "image":
      return `/studio/image/${project.id}`;
    case "trimmer":
      return "/studio/trimmer";
    default:
      return "/dashboard";
  }
}

export function getProjectTypeLabel(type: ProjectType): string {
  switch (type) {
    case "video":
      return "VIDEO";
    case "image":
      return "IMAGE";
    case "trimmer":
      return "TRIMMER";
    default: {
      const _exhaustive: never = type;
      return _exhaustive;
    }
  }
}

export function formatLastEdited(isoDate: string): string {
  const date = new Date(isoDate);
  const now = new Date();
  const diffMs = now.getTime() - date.getTime();
  const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));

  if (diffDays === 0) return "Edited today";
  if (diffDays === 1) return "Edited yesterday";
  if (diffDays < 7) return `Edited ${diffDays} days ago`;

  return date.toLocaleDateString("en-US", {
    month: "short",
    day: "numeric",
    year: date.getFullYear() !== now.getFullYear() ? "numeric" : undefined,
  });
}

export function getProjectThumbnailSrc(seed: string): string {
  return placeholderSrc(seed, 480, 270);
}
