#!/usr/bin/env python3
"""
Generate TypeScript ErrorCode enum from Python ErrorCode enum

Usage:
    python scripts/generate_error_codes_ts.py > ../frontend/src/lib/errors.ts

This script synchronizes the frontend ErrorCode enum with the backend definition
to ensure consistency across the project.
"""

import sys
from pathlib import Path

# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))

from src.utils.errors import ErrorCode


def generate_typescript():
    lines = [
        "import { logger } from './utils/logger';",
        "",
        "/**",
        " * Frontend Error Handling Module",
        " * Provides standardized error codes, user-friendly messages, and retry logic",
        " *",
        " * Error codes are synchronized with backend:",
        " * Format: 4-digit string",
        " * - 0000: Success",
        " * - 1xxx: General errors",
        " * - 2xxx: Business errors",
        " * - 3xxx: Task errors",
        " * - 4xxx: AI service errors",
        " * - 5xxx: Storage errors",
        " */",
        "",
        "/**",
        " * Error codes matching backend error responses",
        " * Auto-synchronized with backend/src/utils/errors.py",
        " */",
        "export enum ErrorCode {",
    ]

    # Group error codes by category for better organization
    categories = {
        "0000": "Success",
        "1": "General errors",
        "2": "Business errors",
        "3": "Task errors",
        "4": "AI service errors",
        "5": "Storage errors",
    }

    current_category = None

    # Add backend error codes
    for code in ErrorCode:
        category = code.value[0] if code.value != "0000" else "0000"

        if category != current_category:
            if current_category is not None:
                lines.append("")
            if category in categories:
                lines.append(f"  // {categories[category]} ({category}xxx)")
            current_category = category

        lines.append(f"  {code.name} = '{code.value}',")

    # Add frontend-specific error codes
    lines.extend([
        "",
        "  // Frontend-specific errors (not from backend)",
        "  NETWORK_ERROR = 'NET01',",
        "  TIMEOUT_ERROR = 'TIM01',",
        "}",
    ])

    return "\n".join(lines)


if __name__ == "__main__":
    print(generate_typescript())
