"""
Bart.dIAs: Main Entry Point

This module serves as the main entry point for the Bart.dIAs system, a Python
assistant that analyzes code to identify and suggest parallelization opportunities.

Features:
- Initializes and coordinates the main components of Bart.dIAs
- Sets up explanations and partitioning suggestions for various code patterns
- Launches the interactive session for code analysis

Components Initialized:
- BDiasParser: For parsing and analyzing Python code
- BDiasCodeGen: For generating parallelization suggestions
- BDiasAssist: For handling user interactions and presenting results

Usage:
Run this script to start an interactive session with Bart.dIAs.
"""

import sys
import json
import argparse
from .bdias_parser import BDiasParser
from .bdias_assist import BDiasAssist
from .bdias_code_gen import BDiasCodeGen
from .analysis import analyze_code
from .constants import EXPLANATIONS, PARTITIONING_SUGGESTIONS


def main():
    # Parse command line arguments
    parser = argparse.ArgumentParser(description="Bart.dIAs: Python Parallelization Assistant")
    parser.add_argument("file", nargs="?", help="Python file or code to analyze")
    parser.add_argument(
        "-t", "--type", 
        choices=["blocks", "blocks_with_suggestions", "critical_path", "patterns", "integrated", "full"], 
        default="integrated",
        help="Type of analysis to perform (default: integrated)"
    )
    parser.add_argument(
        "--format",
        choices=["structured", "markdown"],
        default="markdown",
        help="Output format: 'structured' (JSON dict) or 'markdown' (default: markdown)"
    )
    parser.add_argument(
        "--start-line",
        type=int,
        default=None,
        help="Filter results to start from this line number"
    )
    parser.add_argument(
        "--end-line",
        type=int,
        default=None,
        help="Filter results up to this line number"
    )
    
    args = parser.parse_args()

    # If file is provided, run non-interactive analysis
    if args.file:
        import sys
        print(f"Analyzing {args.file} with mode: {args.type}...\n", file=sys.stderr)
        
        result = analyze_code(
            args.file,
            args.type,
            output_format=args.format,
            start_line=args.start_line,
            end_line=args.end_line
        )
        
        if result["ok"]:
            if args.format == "structured":
                print(json.dumps(result["result"], indent=2, default=str))
            else:
                print(result["result"])
        else:
            print(f"Error: {result['error']}")
            if "details" in result:
                print(f"Details: {result['details']}")
        return

    # Otherwise, run interactive session
    parser_obj = BDiasParser()
    code_generator = BDiasCodeGen(EXPLANATIONS, PARTITIONING_SUGGESTIONS)
    assistant = BDiasAssist(parser_obj, code_generator)
    assistant.run_interactive_session()


if __name__ == "__main__":
    main()
