from rich.prompt import Prompt, IntPrompt
from rich import print

def build_schema(existing_project_names=None):
    if existing_project_names is None:
        existing_project_names = []
        
    print("\n[cyan]--- Schema Builder ---[/cyan]")
    
    while True:
        project_name = Prompt.ask("Enter project name").strip()
        if " " in project_name:
            print("[red]Project name cannot contain spaces. Please use hyphens or underscores.[/red]")
            continue
        if project_name in existing_project_names:
            print("[red]Project name is already taken. Please choose a new one.[/red]")
            continue
        if not project_name:
            print("[red]Project name cannot be empty.[/red]")
            continue
        break
        
    num_tables = IntPrompt.ask("Enter number of tables", default=1)
    
    tables = []
    
    for i in range(num_tables):
        print(f"\n[cyan]Table {i + 1}[/cyan]")
        table_name = Prompt.ask("Enter table name")
        attributes_str = Prompt.ask("Enter attributes (comma separated)")
        
        attributes = [attr.strip() for attr in attributes_str.split(',') if attr.strip()]
        
        tables.append({
            "table_name": table_name,
            "attributes": attributes
        })
        
    additional_requirements = Prompt.ask("Any additional requirements? (Examples: primary keys, foreign keys. Press ENTER if none.)", default="")
    
    return {
        "project_name": project_name,
        "tables": tables,
        "additional_requirements": additional_requirements
    }

def review_schema_options():
    print("\nSelect an option:")
    print("1. Accept schema")
    print("2. Modify schema")
    print("3. Cancel")
    
    choice = Prompt.ask("Choice", choices=["1", "2", "3"])
    return {
        "1": "accept",
        "2": "modify",
        "3": "cancel"
    }[choice]
