#!/usr/bin/env python3
import json
import urllib.parse
import os

# Configuration
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = os.path.join(SCRIPT_DIR, "social_config.json")

def generate_tiktok_auth_url():
    if not os.path.exists(CONFIG_FILE):
        print(f"Error: {CONFIG_FILE} not found.")
        return

    with open(CONFIG_FILE, "r") as f:
        cfg = json.load(f)

    tt_cfg = cfg.get("tiktok", {})
    client_key = tt_cfg.get("client_key")
    
    if not client_key:
        print("Error: TikTok client_key not found in social_config.json")
        return

    # Redirect URI provided by user
    redirect_uri = "https://learn-lexicore.bytronex.com/auth/callback"
    
    # TikTok OAuth Scopes
    scopes = "user.info.basic,video.upload,video.publish"
    
    params = {
        "client_key": client_key,
        "scope": scopes,
        "response_type": "code",
        "redirect_uri": redirect_uri,
        "state": "lexicore_tiktok_auth"
    }
    
    auth_base_url = "https://www.tiktok.com/v2/auth/authorize/"
    full_url = f"{auth_base_url}?{urllib.parse.urlencode(params)}"
    
    print("\nVisit the following URL to authorize TikTok:")
    print("-" * 40)
    print(full_url)
    print("-" * 40)

if __name__ == "__main__":
    generate_tiktok_auth_url()
