#!/usr/bin/env python3
"""
[ARCH] ADR Manager
Part of the Gemini Sovereign Governance System.

Helps create and manage Architecture Decision Records (ADRs).
"""

import os
import sys
import datetime
import argparse

ADR_DIR = "docs/adr"
TEMPLATE_FILE = "docs/adr/ADR_TEMPLATE.md"

class ADRManager:
    def __init__(self):
        # Ensure we are in the repo root or can find docs/adr
        if not os.path.exists(ADR_DIR):
            # Try to find it relative to the script
            script_dir = os.path.dirname(os.path.abspath(__file__))
            repo_root = os.path.dirname(script_dir)
            self.adr_dir = os.path.join(repo_root, ADR_DIR)
            self.template_file = os.path.join(repo_root, TEMPLATE_FILE)
        else:
            self.adr_dir = ADR_DIR
            self.template_file = TEMPLATE_FILE

        if not os.path.exists(self.adr_dir):
            os.makedirs(self.adr_dir)

    def get_next_number(self):
        files = [f for f in os.listdir(self.adr_dir) if f.endswith(".md") and f[0].isdigit()]
        if not files:
            return 1
        numbers = [int(f.split("-")[0]) for f in files]
        return max(numbers) + 1

    def create_adr(self, title: str):
        num = self.get_next_number()
        slug = title.lower().replace(" ", "-").replace("/", "-")
        filename = f"{num:04d}-{slug}.md"
        filepath = os.path.join(self.adr_dir, filename)
        
        date = datetime.date.today().isoformat()
        
        template = ""
        if os.path.exists(self.template_file):
            with open(self.template_file, "r") as f:
                template = f.read()
        else:
            template = """# {num}. {title}

Date: {date}

## Status

Proposed

## Context

What is the problem we are solving?

## Decision

What is the decision we made?

## Consequences

What are the positive and negative consequences?
"""

        content = template.replace("{num}", f"{num:04d}")
        content = content.replace("{title}", title)
        content = content.replace("{date}", date)
        
        with open(filepath, "w") as f:
            f.write(content)
            
        print(f"[SUCCESS] Created ADR: {filepath}")

    def list_adrs(self):
        files = sorted([f for f in os.listdir(self.adr_dir) if f.endswith(".md") and f[0].isdigit()])
        print(f"{'NUM':<6} {'TITLE'}")
        print("-" * 40)
        for f in files:
            num = f.split("-")[0]
            title = f.split("-", 1)[1].replace(".md", "").replace("-", " ").title()
            print(f"{num:<6} {title}")

def main():
    parser = argparse.ArgumentParser(description="Gemini ADR Manager")
    subparsers = parser.add_subparsers(dest="command")
    
    create_parser = subparsers.add_parser("new", help="Create a new ADR")
    create_parser.add_argument("title", help="Title of the ADR")
    
    subparsers.add_parser("list", help="List all ADRs")
    
    args = parser.parse_args()
    
    manager = ADRManager()
    
    if args.command == "new":
        manager.create_adr(args.title)
    elif args.command == "list":
        manager.list_adrs()
    else:
        parser.print_help()

if __name__ == "__main__":
    main()
