#!/usr/bin/env python3
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
PaddleOCR Document Parsing Configuration Wizard

Supports two modes:
1. Interactive mode (default): python configure.py
2. CLI mode: python configure.py --api-url URL --token TOKEN

Interactive configuration for PaddleOCR document parsing API credentials.
Saves configuration to .env file in project root.

Get your API credentials at: https://paddleocr.com
"""

import argparse
import os
import sys
from pathlib import Path


def _read_env_config(env_file: Path) -> dict:
    """Read key/value pairs from .env file."""
    config = {}
    if not env_file.exists():
        return config

    with open(env_file, "r", encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith("#") and "=" in line:
                key, value = line.split("=", 1)
                config[key.strip()] = value.strip()
    return config


def save_config(
    api_url: str, token: str, project_root: Path, quiet: bool = False
) -> bool:
    """
    Save configuration to .env file

    Args:
        api_url: Document parsing API URL
        token: Access token
        project_root: Project root directory
        quiet: If True, suppress output messages

    Returns:
        True if successful, False otherwise
    """
    env_file = project_root / ".env"

    # Read existing .env if it exists
    existing_config = {}
    if env_file.exists():
        if not quiet:
            print(f"Found existing .env file: {env_file}")
            overwrite = input("Overwrite? [Y/n]: ").strip().lower()
            if overwrite == "n":
                print("Configuration cancelled")
                return False

        existing_config = {
            key: value
            for key, value in _read_env_config(env_file).items()
            if key
            not in [
                "PADDLEOCR_DOC_PARSING_API_URL",
                "PADDLEOCR_ACCESS_TOKEN",
            ]
        }

    # Write to .env file
    try:
        with open(env_file, "w", encoding="utf-8") as f:
            # Write header
            f.write("# PaddleOCR Skills Configuration\n")
            f.write("# Generated by configuration wizard\n")
            f.write("# Get your API credentials at: https://paddleocr.com\n")
            f.write("\n")

            # Document Parsing configs
            f.write("# ========================================\n")
            f.write("# PaddleOCR Document Parsing Configuration\n")
            f.write("# ========================================\n")
            f.write(f"PADDLEOCR_DOC_PARSING_API_URL={api_url}\n")
            f.write(f"PADDLEOCR_ACCESS_TOKEN={token}\n")
            f.write("\n")

            # Write other configs
            if existing_config:
                f.write("# ========================================\n")
                f.write("# Other Configuration\n")
                f.write("# ========================================\n")
                for key, value in existing_config.items():
                    f.write(f"{key}={value}\n")

        if not quiet:
            print(f"[OK] Configuration saved to {env_file}")
        return True

    except Exception as e:
        print(f"[FAIL] Failed to save configuration: {e}")
        return False


def main():
    # Parse command-line arguments
    parser = argparse.ArgumentParser(
        description="PaddleOCR Document Parsing Configuration Tool",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Interactive mode
  python configure.py

  # CLI mode (non-interactive)
  python configure.py --api-url "https://your-service.paddleocr.com/layout-parsing" --token "your_token"

Get your API credentials at: https://paddleocr.com
        """,
    )
    parser.add_argument(
        "--api-url", help="Document parsing API URL (non-interactive mode)"
    )
    parser.add_argument("--token", help="Access token (non-interactive mode)")
    parser.add_argument("--quiet", action="store_true", help="Suppress output messages")

    args = parser.parse_args()

    # Find .env file location (project root, 2 levels up from script)
    project_root = Path(__file__).parent.parent.parent

    # ========================================
    # CLI Mode (non-interactive)
    # ========================================
    if args.api_url and args.token:
        try:
            api_url = args.api_url.strip()
            token = args.token.strip()

            # Validate URL format
            if not api_url.startswith(("http://", "https://")):
                api_url = f"https://{api_url}"

            # Validate token
            if len(token) < 16:
                print("Error: Token seems too short. Please check and try again.")
                sys.exit(1)

            # Save configuration (CLI mode always overwrites without asking)
            if save_config(api_url, token, project_root, quiet=True):
                if not args.quiet:
                    masked_token = (
                        token[:8] + "..." + token[-4:] if len(token) > 12 else "***"
                    )
                    print("\n[OK] Configuration complete!")
                    print(f"  PADDLEOCR_DOC_PARSING_API_URL: {api_url}")
                    print(f"  PADDLEOCR_ACCESS_TOKEN: {masked_token}")
                sys.exit(0)
            else:
                sys.exit(1)

        except Exception as e:
            print(f"Error: {e}")
            sys.exit(1)

    elif args.api_url or args.token:
        print("Error: Both --api-url and --token are required for CLI mode")
        print("Run without arguments for interactive mode")
        sys.exit(1)

    # ========================================
    # Interactive Mode
    # ========================================
    print("=" * 60)
    print("PaddleOCR Document Parsing - Configuration Wizard")
    print("=" * 60)
    print("\nGet your API credentials at: https://paddleocr.com")
    print()

    env_file = project_root / ".env"
    print(f"Configuration will be saved to: {env_file}")
    print()

    # Read existing .env if it exists
    existing_config = {}
    if env_file.exists():
        print("Found existing .env file, loading current values...")
        existing_config = _read_env_config(env_file)
        print()

    # Get current values
    current_api_url = existing_config.get("PADDLEOCR_DOC_PARSING_API_URL", "")
    current_token = existing_config.get("PADDLEOCR_ACCESS_TOKEN", "")

    print("Please provide your PaddleOCR document parsing API credentials:")
    print("(Press Enter to keep current value)")
    print()

    # Prompt for API URL
    print("1. PADDLEOCR_DOC_PARSING_API_URL - Document parsing API endpoint")
    print("   Example: https://your-service.paddleocr.com/layout-parsing")
    if current_api_url:
        print(f"   Current: {current_api_url}")

    api_url_input = input("   Enter PADDLEOCR_DOC_PARSING_API_URL: ").strip()
    new_api_url = api_url_input if api_url_input else current_api_url

    if not new_api_url:
        print()
        print("ERROR: PADDLEOCR_DOC_PARSING_API_URL is required.")
        print("Please run this wizard again and provide a valid API URL.")
        sys.exit(1)

    print()

    # Prompt for Token
    print("2. PADDLEOCR_ACCESS_TOKEN - Your access token")
    if current_token:
        masked_token = (
            current_token[:8] + "..." + current_token[-4:]
            if len(current_token) > 12
            else "***"
        )
        print(f"   Current: {masked_token}")

    token_input = input("   Enter PADDLEOCR_ACCESS_TOKEN: ").strip()
    new_token = token_input if token_input else current_token

    if not new_token:
        print()
        print("ERROR: PADDLEOCR_ACCESS_TOKEN is required.")
        print("Please run this wizard again and provide a valid token.")
        sys.exit(1)

    print()

    # Save configuration
    print("Saving configuration...")

    if not save_config(new_api_url, new_token, project_root):
        sys.exit(1)

    print()

    # Verify configuration
    print("Verifying configuration...")
    try:
        sys.path.insert(0, str(Path(__file__).parent))
        from lib import get_config

        api_url, token = get_config()

        print("[OK] PADDLEOCR_DOC_PARSING_API_URL loaded successfully")
        print("[OK] PADDLEOCR_ACCESS_TOKEN loaded successfully")
        print()

    except Exception as e:
        print(f"[FAIL] Configuration verification failed: {e}")
        print()
        sys.exit(1)

    # Next steps
    print("=" * 60)
    print("Configuration Complete!")
    print("=" * 60)
    print()
    print("Next steps:")
    print("  1. Test the configuration:")
    print("     python scripts/paddleocr-doc-parsing/smoke_test.py")
    print()
    print("  2. Try parsing a document:")
    print('     python scripts/paddleocr-doc-parsing/vl_caller.py --file-url "URL"')
    print()


if __name__ == "__main__":
    main()
