import sys
from pathlib import Path

# Add project root (GenAIResultsComparator/) to sys.path
# Path(__file__) is GenAIResultsComparator/scripts/generate_pypi_description.py
# .parent is GenAIResultsComparator/scripts/
# .parent.parent is GenAIResultsComparator/
PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))

try:
    # Now we can import from the 'scripts' package
    from scripts.readme_parser import extract_section, get_readme_content
except ImportError as e:
    print(f"Error: Could not import 'scripts.readme_parser'. Details: {e}")
    print(f"PROJECT_ROOT: {PROJECT_ROOT}")
    print(f"sys.path: {sys.path}")
    sys.exit(1)
finally:
    # Clean up sys.path
    if str(PROJECT_ROOT) in sys.path:
        sys.path.remove(str(PROJECT_ROOT))


PYPI_DESC_PATH = PROJECT_ROOT / "PYPI_DESCRIPTION.md"


def build_pypi_description():
    readme_content = get_readme_content()

    # Extract required sections from README.md
    # Note: section keys are case-insensitive for extract_section due to .upper() in marker definition
    tagline = extract_section(readme_content, "TAGLINE")
    desc_core_concept = extract_section(readme_content, "DESCRIPTION_CORE_CONCEPT")
    quickstart_code = extract_section(readme_content, "QUICKSTART_CODE")
    features_list = extract_section(readme_content, "FEATURES_LIST")

    # Installation Sections
    install_standard_intro = extract_section(readme_content, "INSTALLATION_STANDARD_INTRO")
    install_standard_setup = extract_section(readme_content, "INSTALLATION_STANDARD_SETUP")
    install_pypi_basic = extract_section(
        readme_content, "INSTALLATION_PYPI_BASIC"
    )  # This is the core pip install command
    install_jupyter_guide = extract_section(readme_content, "INSTALLATION_JUPYTER_GUIDE")
    install_optional_intro = extract_section(readme_content, "INSTALLATION_OPTIONAL_INTRO")
    install_optional_features = extract_section(readme_content, "INSTALLATION_OPTIONAL_FEATURES")
    install_size_table_intro = extract_section(readme_content, "INSTALLATION_SIZE_TABLE_INTRO")
    install_size_table_content = extract_section(readme_content, "INSTALLATION_SIZE_TABLE_CONTENT")
    install_developer_guide = extract_section(readme_content, "INSTALLATION_DEVELOPER_GUIDE")

    citation_content = extract_section(readme_content, "CITATION_CONTENT")

    # Assemble PYPI_DESCRIPTION.md content
    # We'll structure the installation section logically for PyPI.
    content = f"""\
<!-- This file is generated by root's scripts/generate_pypi_description.py -->

# GAICo: GenAI Results Comparator

**Repository:** [github.com/ai4society/GenAIResultsComparator](https://github.com/ai4society/GenAIResultsComparator)

**Documentation:** [ai4society.github.io/projects/GenAIResultsComparator](https://ai4society.github.io/projects/GenAIResultsComparator/index.html)

## Overview

{tagline.strip()}

{desc_core_concept.strip()}

## Quickstart

GAICo's `Experiment` class offers a streamlined workflow for comparing multiple model outputs, applying thresholds, generating plots, and creating CSV reports.

Here's a quick example:

{quickstart_code.strip()}

For more detailed examples, please refer to our Jupyter Notebooks in the [`examples/`](https://github.com/ai4society/GenAIResultsComparator/tree/main/examples) folder in the repository.

## Features

{features_list.strip()}

## Installation

{install_standard_intro.strip()}

{install_standard_setup.strip()}

{install_pypi_basic.strip()}

{install_jupyter_guide.strip()}

{install_optional_intro.strip()}

{install_optional_features.strip()}

{install_size_table_intro.strip()}
{install_size_table_content.strip()}

{install_developer_guide.strip()}


## Citation

If you find GAICo useful in your research or work, please consider citing it:

{citation_content.strip()}
"""
    PYPI_DESC_PATH.write_text(content, encoding="utf-8")
    print(f"Successfully generated {PYPI_DESC_PATH}")


if __name__ == "__main__":
    build_pypi_description()
