#!/usr/bin/env python3
"""
Test script to verify LinkedIn auto-posting setup.

Usage:
    python scripts/test_linkedin_setup.py
"""
import asyncio
import sys
from pathlib import Path

# Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent))

from src.config import settings
from src.database.client import db


async def main():
    """Run setup checks."""
    print("=" * 70)
    print("LinkedIn Auto-Posting Setup Verification")
    print("=" * 70)
    print()

    checks_passed = 0
    checks_total = 0

    # Check 1: Environment variables
    print("📋 Checking environment variables...")
    checks_total += 4

    if settings.linkedin_client_id:
        print("  ✅ LINKEDIN_CLIENT_ID is set")
        checks_passed += 1
    else:
        print("  ❌ LINKEDIN_CLIENT_ID is missing")

    if settings.linkedin_client_secret:
        print("  ✅ LINKEDIN_CLIENT_SECRET is set")
        checks_passed += 1
    else:
        print("  ❌ LINKEDIN_CLIENT_SECRET is missing")

    if settings.linkedin_redirect_uri:
        print("  ✅ LINKEDIN_REDIRECT_URI is set")
        checks_passed += 1
        print(f"     URI: {settings.linkedin_redirect_uri}")
    else:
        print("  ❌ LINKEDIN_REDIRECT_URI is missing")

    if settings.encryption_key:
        print("  ✅ ENCRYPTION_KEY is set")
        checks_passed += 1
    else:
        print("  ❌ ENCRYPTION_KEY is missing")

    print()

    # Check 2: Encryption
    print("🔐 Testing encryption...")
    checks_total += 1

    try:
        from src.utils.encryption import encrypt_token, decrypt_token
        test_token = "test_access_token_12345"
        encrypted = encrypt_token(test_token)
        decrypted = decrypt_token(encrypted)

        if decrypted == test_token:
            print("  ✅ Encryption/decryption working")
            checks_passed += 1
        else:
            print("  ❌ Encryption/decryption mismatch")
    except Exception as e:
        print(f"  ❌ Encryption error: {e}")

    print()

    # Check 3: Database table
    print("💾 Checking database schema...")
    checks_total += 1

    try:
        # Try to query the table (will fail if it doesn't exist)
        result = await asyncio.to_thread(
            lambda: db.client.table("linkedin_accounts").select("id").limit(0).execute()
        )
        print("  ✅ linkedin_accounts table exists")
        checks_passed += 1
    except Exception as e:
        print(f"  ❌ linkedin_accounts table not found: {e}")
        print("     Run: psql $DATABASE_URL -f config/migrate_add_linkedin_accounts.sql")

    print()

    # Check 4: LinkedIn service
    print("🔧 Checking LinkedIn service...")
    checks_total += 1

    try:
        from src.services.linkedin_service import linkedin_service
        print("  ✅ LinkedIn service initialized")
        checks_passed += 1
    except Exception as e:
        print(f"  ❌ LinkedIn service error: {e}")

    print()

    # Summary
    print("=" * 70)
    print(f"Summary: {checks_passed}/{checks_total} checks passed")
    print("=" * 70)

    if checks_passed == checks_total:
        print("✅ All checks passed! LinkedIn auto-posting is ready.")
        print("\nNext steps:")
        print("1. Restart your application")
        print("2. Log in as an employee")
        print("3. Go to Settings and connect your LinkedIn account")
        print("4. Schedule a test post")
        return 0
    else:
        print("❌ Some checks failed. Please fix the issues above.")
        print("\nSetup guide: See LINKEDIN_SETUP.md for detailed instructions")
        return 1


if __name__ == "__main__":
    exit_code = asyncio.run(main())
    sys.exit(exit_code)
