"""
CLI entry point for notion-bulk-export.
"""

import argparse
import asyncio
import os
import sys

from notion_bulk_export.exporter import NotionExporter


def main() -> None:
    parser = argparse.ArgumentParser(
        prog="notion-bulk-export",
        description="Fast bulk export for Notion databases with full page content.",
    )
    parser.add_argument(
        "--database-id",
        required=True,
        help="Notion database ID (from the URL)",
    )
    parser.add_argument(
        "--token",
        default=os.environ.get("NOTION_TOKEN"),
        help="Notion integration token (default: $NOTION_TOKEN)",
    )
    parser.add_argument(
        "--output",
        default="./output",
        help="Output directory (default: ./output)",
    )
    parser.add_argument(
        "--format",
        nargs="+",
        choices=["json", "csv"],
        default=["json"],
        help="Output format(s) (default: json)",
    )
    parser.add_argument(
        "--concurrency",
        type=int,
        default=5,
        help="Number of parallel API requests (default: 5)",
    )
    parser.add_argument(
        "--no-content",
        action="store_true",
        help="Skip page content (export properties only)",
    )

    args = parser.parse_args()

    if not args.token:
        print(
            "[ERROR] Notion token required. Use --token or set NOTION_TOKEN"
            " environment variable."
        )
        print()
        print("  Get a token at: https://www.notion.so/my-integrations")
        sys.exit(1)

    # Normalize database ID (remove dashes if present)
    database_id = args.database_id.replace("-", "")
    if len(database_id) == 32:
        database_id = (
            f"{database_id[:8]}-{database_id[8:12]}-{database_id[12:16]}"
            f"-{database_id[16:20]}-{database_id[20:]}"
        )

    exporter = NotionExporter(
        token=args.token,
        database_id=database_id,
        output_dir=args.output,
        concurrency=args.concurrency,
        include_content=not args.no_content,
        formats=args.format,
    )

    asyncio.run(exporter.run())
