# Trend Analysis - Instagram Integration

## Goal Description
Integrate Instagram data into the Trend Analysis feature. This will allow users to track trends for Instagram Audio (Reels) alongside TikTok Sounds. We will use Apify's Instagram Scraper to fetch data.

## User Review Required
> [!IMPORTANT]
> **Apify Actor Selection**: I plan to use `apify/instagram-scraper` (or similar) for fetching Instagram data. Please confirm if you have a specific Actor preference or if extensive configuration is needed (e.g., cookies/login).
> **Token Usage**: Ensure your Apify token has access to the chosen Instagram Actor.

## Proposed Changes

### UI
#### [MODIFY] [TrendAnalysis.tsx](file:///Users/mmachado/Library/CloudStorage/OneDrive-SonyMusicEntertainment/Documents/collab/mymac80/apify-tiktok/ui/src/pages/TrendAnalysis.tsx)
- Add a **Platform Selector** (Toggle/Tabs) for TikTok vs Instagram.
- Update input label to be dynamic (e.g., "TikTok Sound ID" vs "Instagram Audio ID").
- Update `handleTrack` to call the appropriate API function based on the selected platform.
- Pass `platform` context to analytics to handle different field mappings if necessary.

### API
#### [MODIFY] [apify.ts](file:///Users/mmachado/Library/CloudStorage/OneDrive-SonyMusicEntertainment/Documents/collab/mymac80/apify-tiktok/ui/src/api/apify.ts)
- Add `INSTAGRAM_SCRAPER_ACTOR` constant.
- Add `fetchInstagramAudioDetails(audioIdOrUrl: string)` function.
  - Logic to format input for the Instagram actor.
  - Logic to map Instagram actor output to a common format (or return as is and handle in analytics).

#### [MODIFY] [analytics.ts](file:///Users/mmachado/Library/CloudStorage/OneDrive-SonyMusicEntertainment/Documents/collab/mymac80/apify-tiktok/ui/src/api/analytics.ts)
- Update `calculateTrendMetrics` to handle Instagram data fields.
  - Map Instagram's `likesCount` -> `diggCount` (or standardize both to `likes`).
  - Map `videoViewCount` -> `playCount` (or standardize both to `views`).
  - Map `timestamp` -> `createTime`.
- Update `aggregateDaily` to handle different timestamp formats if Instagram uses ISO strings vs TikTok's Unix timestamps.

#### [MODIFY] [db.ts](file:///Users/mmachado/Library/CloudStorage/OneDrive-SonyMusicEntertainment/Documents/collab/mymac80/apify-tiktok/ui/src/api/db.ts)
- Ensure the cache schema supports platform differentiation (e.g. composite key `platform` + `id` or just prefix the ID in the cache). *Currently keys are just lowercase IDs, which might collide if IDs overlap, though unlikely. Safest to prefix like `tiktok:123` or `ig:123`.*

## Verification Plan

### Manual Verification
1.  **Start the App**: `npm run dev` in `ui/`.
2.  **Select Instagram**: Toggle the platform to Instagram in Trend Analysis.
3.  **Input Audio ID**: Enter a valid Instagram Audio ID or URL.
    -   *User needs to provide a sample ID for testing.*
4.  **Track**: Click "Track Trend".
5.  **Verify Results**:
    -   Check if data loads without error.
    -   Verify Charts and Tables populate correctly.
    -   Check History Panel updates with the new entry.
    -   Check Cache functionality (reload and see if it loads from cache).
