#!/usr/bin/env python3
"""
Multiple Asset Manager for NotebookLM

Handles multiple audio and video files from the same notebook.
Provides manual download instructions and database update scripts.
"""
import os
import sys

def main():
    print("=" * 80)
    print("MULTIPLE NOTEBOOKLM ASSETS MANAGER")
    print("=" * 80)

    notebook_id = "0f38d000-7497-442c-9b67-8773b02a558b"
    report_id = 35
    nlm_account_id = 10
    output_dir = f"/home/ashraffarid2010/jadwaai.com/app/static/uploads/notebooks/{report_id}"
    notebook_url = f"https://notebooklm.google.com/notebook/{notebook_id}"

    print(f"\n📋 Configuration:")
    print(f"  Report ID: {report_id}")
    print(f"  Notebook URL: {notebook_url}")
    print(f"  NLM Account ID: {nlm_account_id}")
    print(f"  Output Directory: {output_dir}")

    print(f"\n🔍 Current Situation:")
    print(f"  User reports: 3 audio files + 3 video files in NotebookLM")
    print(f"  Current system: Only supports 1 of each asset type")
    print(f"  Current files: {len(os.listdir(output_dir)) if os.path.exists(output_dir) else 0}")

    print(f"\n🎯 Solution Options:")

    print(f"\n1️⃣  MANUAL DOWNLOAD (Recommended for now):")
    print(f"   a) Open Notebook: {notebook_url}")
    print(f"   b) For each audio/video:")
    print(f"      - Click on the artifact")
    print(f"      - Look for download button")
    print(f"      - Download with numbered names:")
    print(f"        • audio_1.mp3, audio_2.mp3, audio_3.mp3")
    print(f"        • video_1.mp4, video_2.mp4, video_3.mp4")
    print(f"   c) Upload to: {output_dir}")

    print(f"\n2️⃣  AUTOMATIC DOWNLOAD (Development):")
    print(f"   Create enhanced download script that:")
    print(f"   - Lists ALL artifacts in notebook")
    print(f"   - Downloads each with sequential numbering")
    print(f"   - Updates database for each file")

    print(f"\n3️⃣  SYSTEM ARCHITECTURE UPDATE:")
    print(f"   Modify database schema to support multiple assets:")
    print(f"   - Add 'asset_index' field to notebook_assets table")
    print(f"   - Allow multiple assets per type per report")
    print(f"   - Update UI to show all available assets")

    print(f"\n📊 DATABASE UPDATE FOR MULTIPLE ASSETS:")

    # SQL commands for adding multiple assets
    sql_commands = f"""
-- Enable multiple assets per type per report
-- First, check current assets
SELECT asset_type, COUNT(*) as count
FROM notebook_assets
WHERE report_id = {report_id}
GROUP BY asset_type;

-- Add new assets for additional audio files
INSERT INTO notebook_assets (report_id, asset_type, status, file_path, file_format, created_at, completed_at)
VALUES
  ({report_id}, 'audio', 'ready', 'uploads/notebooks/{report_id}/audio_2.mp3', 'mp3', NOW(), NOW()),
  ({report_id}, 'audio', 'ready', 'uploads/notebooks/{report_id}/audio_3.mp3', 'mp3', NOW(), NOW());

-- Add new assets for video files
INSERT INTO notebook_assets (report_id, asset_type, status, file_path, file_format, created_at, completed_at)
VALUES
  ({report_id}, 'video', 'ready', 'uploads/notebooks/{report_id}/video_1.mp4', 'mp4', NOW(), NOW()),
  ({report_id}, 'video', 'ready', 'uploads/notebooks/{report_id}/video_2.mp4', 'mp4', NOW(), NOW()),
  ({report_id}, 'video', 'ready', 'uploads/notebooks/{report_id}/video_3.mp4', 'mp4', NOW(), NOW());

-- Update existing failed video asset
UPDATE notebook_assets
SET status = 'ready',
    file_path = 'uploads/notebooks/{report_id}/video.mp4',
    file_format = 'mp4',
    error_message = NULL,
    completed_at = NOW()
WHERE report_id = {report_id}
  AND asset_type = 'video'
  AND id = (SELECT MIN(id) FROM notebook_assets WHERE report_id = {report_id} AND asset_type = 'video');
    """

    print(sql_commands)

    print(f"\n🔧 Quick Manual Download Steps:")

    steps = [
        "1. Open NotebookLM URL",
        "2. Look for 'Audio Overview' section - should see 3 audio files",
        "3. For each audio file:",
        "   - Click the three dots menu",
        "   - Select 'Download'",
        "   - Save as audio_1.mp3, audio_2.mp3, audio_3.mp3",
        "4. Look for 'Video Overview' section - should see 3 video files",
        "5. For each video file:",
        "   - Click the three dots menu",
        "   - Select 'Download'",
        "   - Save as video_1.mp4, video_2.mp4, video_3.mp4",
        "6. Upload all files to server directory:",
        f"   {output_dir}",
        "7. Run the SQL commands above to update database",
    ]

    for i, step in enumerate(steps, 1):
        print(f"  {step}")

    print(f"\n📁 File Naming Convention:")
    print(f"  🎧 Audio files: audio_1.mp3, audio_2.mp3, audio_3.mp3")
    print(f"  🎬 Video files: video_1.mp4, video_2.mp4, video_3.mp4")
    print(f"  📁 Upload to: {output_dir}")

    print(f"\n🎯 Expected Final State:")
    print(f"  📊 Current: 7 files (7 asset types)")
    print(f"  ✅ Target: 12 files (3 audio + 3 video + 6 other types)")
    print(f"  📈 Total assets: 12 instead of 8")

    print(f"\n💡 Alternative Approach:")
    print(f"  If downloading manually is too complex, you can:")
    print(f"  1. Share the NotebookLM notebook link with users")
    print(f"  2. Let users access all assets directly in NotebookLM")
    print(f"  3. Update Jadwa AI to show 'View in NotebookLM' button")
    print(f"  4. No need to download/store all versions locally")

    print(f"\n🔗 Direct Access Link:")
    print(f"  {notebook_url}")

    print(f"\n" + "=" * 80)

if __name__ == "__main__":
    main()