import { ActivityPubNote, ActivityPubOutbox, EpisodeResponse } from "./types";

export function transformEpisode(response: EpisodeResponse) {
  const item = response.item;
  return {
    "@context": "https://www.w3.org/ns/activitystreams",
    "id": item.assetId,
    "type": "PodcastEpisode",
    "published": item.published,
    "updated": item.updated,
    "attributedTo": item.show.coreId,
    "externalId": item.url,
    "title": item.title,
    "description": {
      "type": "Note",
      "mediaType": "text/markdown",
      "content": item.description
    },
    "image": item.imagesList.length > 0 ? {
      "type": "Image",
      "url": item.imagesList[0].url
    } : undefined,
    "audio": item.audioList.map(audio => ({
      "type": "Audio",
      "name": audio.title,
      "mediaType": "audio/mpeg",
      "url": audio.href
    })),
    "partOf": {
      "type": "PodcastSeries",
      "id": item.show.coreId,
      "externalId": item.show.externalId,
      "name": item.show.title,
      "attributedTo": item.show.publisher.coreId,
      "publisher": {
        "type": "BroadcastService",
        "name": item.show.publisher.title,
        "url": item.show.publisher.url,
        "organization": {
          "type": "Organization",
          "name": item.show.publisher.organization.name,
          "url": item.show.publisher.organization.url
        }
      }
    }
  };
}

export function transformEpisodeList(input: any): ActivityPubOutbox {
    const baseUrl = "https://example.com/outbox";
    const actorUrl = "https://example.com/actor";
    
    const orderedItems: ActivityPubNote[] = input.show.items.nodes.map((item: any) => ({
      id: item.assetId,
      type: "Note",
      actor: actorUrl,
      published: new Date().toISOString(),
      to: ["https://www.w3.org/ns/activitystreams#Public"],
      content: item.title,
      url: item.url,
    }));
    
    return {
      "@context": "https://www.w3.org/ns/activitystreams",
      id: baseUrl,
      type: "OrderedCollection",
      orderedItems,
    };
  }