#!/usr/bin/env python3
"""One-off Instagram audit for @metowi and @bleutiquehair."""

import json, os, sys, time, requests
from pathlib import Path
from dotenv import load_dotenv

ROOT = Path(__file__).parent.parent
load_dotenv(ROOT / ".env")

APIFY_KEY = os.getenv("APIFY_API_KEY")
APIFY_BASE = "https://api.apify.com/v2"
TIMEOUT = 180

def apify_run(actor, input_data):
    resp = requests.post(
        f"{APIFY_BASE}/acts/{actor}/runs",
        params={"token": APIFY_KEY},
        json=input_data,
        timeout=30,
    )
    resp.raise_for_status()
    run = resp.json()["data"]
    run_id = run["id"]
    dataset_id = run["defaultDatasetId"]
    print(f"  Run started: {run_id}")

    deadline = time.time() + TIMEOUT
    while time.time() < deadline:
        sr = requests.get(f"{APIFY_BASE}/actor-runs/{run_id}", params={"token": APIFY_KEY}, timeout=15)
        sr.raise_for_status()
        status = sr.json()["data"]["status"]
        if status == "SUCCEEDED":
            break
        if status in ("FAILED", "ABORTED", "TIMED-OUT"):
            raise RuntimeError(f"Run {run_id} ended: {status}")
        print(f"  waiting... ({status})")
        time.sleep(8)
    else:
        raise TimeoutError("Timed out waiting for Apify run")

    items = requests.get(
        f"{APIFY_BASE}/datasets/{dataset_id}/items",
        params={"token": APIFY_KEY, "format": "json"},
        timeout=30,
    ).json()
    return items


def analyze(handle):
    print(f"\n{'='*50}")
    print(f"Scraping @{handle} ...")
    items = apify_run("apify~instagram-profile-scraper", {
        "usernames": [handle],
        "resultsLimit": 12,
    })
    if not items:
        print(f"  No data returned.")
        return

    p = items[0]

    followers   = p.get("followersCount", 0) or 0
    following   = p.get("followsCount", 0) or 0
    posts_count = p.get("postsCount", 0) or 0
    bio         = p.get("biography", "") or ""
    is_private  = p.get("isPrivate", False)
    is_verified = p.get("verified", False)
    profile_url = p.get("url", f"https://instagram.com/{handle}")
    full_name   = p.get("fullName", "")
    category    = p.get("businessCategoryName", "") or p.get("categoryName", "") or ""
    website     = p.get("externalUrl", "") or ""

    recent_posts = p.get("latestPosts", []) or []
    if recent_posts:
        likes_list    = [x.get("likesCount", 0) or 0 for x in recent_posts]
        comments_list = [x.get("commentsCount", 0) or 0 for x in recent_posts]
        avg_likes     = round(sum(likes_list) / len(likes_list), 1)
        avg_comments  = round(sum(comments_list) / len(comments_list), 1)
        max_likes     = max(likes_list)
        min_likes     = min(likes_list)
        types         = [x.get("type", "") for x in recent_posts]
        reel_count    = sum(1 for t in types if "video" in t.lower() or "reel" in t.lower())
        timestamps    = [x.get("timestamp", "") for x in recent_posts if x.get("timestamp")]
    else:
        avg_likes = avg_comments = max_likes = min_likes = reel_count = 0
        timestamps = []

    engagement_pct = round((avg_likes + avg_comments) / followers * 100, 2) if followers > 0 else 0.0
    ff_ratio = round(followers / following, 1) if following > 0 else None

    print(f"\n--- @{handle} ({full_name}) ---")
    print(f"  Profile:     {profile_url}")
    print(f"  Category:    {category}")
    print(f"  Private:     {is_private}  |  Verified: {is_verified}")
    print(f"  Bio:         {bio[:120]}")
    print(f"  Website:     {website}")
    print(f"\n  Followers:   {followers:,}")
    print(f"  Following:   {following:,}")
    print(f"  F/F Ratio:   {ff_ratio}x")
    print(f"  Total Posts: {posts_count:,}")
    print(f"\n  Avg Likes:     {avg_likes}")
    print(f"  Avg Comments:  {avg_comments}")
    print(f"  Engagement %:  {engagement_pct}%")
    print(f"  Likes range:   {min_likes} – {max_likes}")
    print(f"  Reels (of {len(recent_posts)} recent): {reel_count}")
    if timestamps:
        print(f"  Most recent post: {timestamps[0][:10]}")
        print(f"  Oldest in sample: {timestamps[-1][:10]}")

    # ── Flag issues ──
    flags = []

    if is_private:
        flags.append("CRITICAL: Profile is private — no public reach, blocks discovery")
    if followers < 500:
        flags.append(f"LOW FOLLOWERS: {followers:,} — very early stage, low social proof")
    elif followers < 1500:
        flags.append(f"SMALL AUDIENCE: {followers:,} followers — limited organic reach")
    if engagement_pct < 1.0:
        flags.append(f"LOW ENGAGEMENT: {engagement_pct}% — industry healthy range is 2–5%")
    elif engagement_pct < 2.0:
        flags.append(f"BELOW-AVERAGE ENGAGEMENT: {engagement_pct}% — aim for 2%+")
    if posts_count < 12:
        flags.append(f"FEW POSTS: only {posts_count} total — thin content library")
    if not bio.strip():
        flags.append("MISSING BIO — no description to convert visitors")
    elif len(bio) < 40:
        flags.append(f"WEAK BIO: too short ({len(bio)} chars) — not communicating value")
    if not website:
        flags.append("NO WEBSITE LINK in bio — missing key conversion point")
    if not category:
        flags.append("NO BUSINESS CATEGORY set — missing discoverability signal")
    if ff_ratio is not None and ff_ratio < 1.0:
        flags.append(f"FOLLOWING MORE THAN FOLLOWERS (ratio {ff_ratio}x) — looks spammy")
    if reel_count == 0 and len(recent_posts) > 0:
        flags.append("NO REELS in recent posts — Reels get 2–3x more reach on IG algo")
    if avg_likes < 10 and followers > 200:
        flags.append(f"VERY LOW LIKES ({avg_likes} avg) relative to follower count — possible ghost followers or shadow-ban")

    print(f"\n  FLAGS ({len(flags)}):")
    if flags:
        for f in flags:
            print(f"    ⚠  {f}")
    else:
        print("    ✓ No major issues found")

    # Save raw dump
    out_path = ROOT / ".tmp" / f"ig_audit_{handle}.json"
    out_path.write_text(json.dumps(p, indent=2, default=str))
    print(f"\n  Raw data saved: {out_path}")


if __name__ == "__main__":
    for h in ["metowi", "bleutiquehair"]:
        analyze(h)
    print("\nDone.")
