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70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
"""Setup wizard commands."""

import asyncio
import secrets
import typer
from rich.console import Console
from rich.panel import Panel
from rich.table import Table

console = Console()
app = typer.Typer(help="First-time setup wizard.")


def run_async(coro):
    """Run async function in sync context."""
    return asyncio.get_event_loop().run_until_complete(coro)


def generate_password(length: int = 16) -> str:
    """Generate a secure random password."""
    import string

    alphabet = string.ascii_letters + string.digits + "!@#$%^&*"
    return "".join(secrets.choice(alphabet) for _ in range(length))


@app.command()
def wizard(
    non_interactive: bool = typer.Option(
        False, "--non-interactive", help="Non-interactive mode for automation"
    ),
    admin_email: str = typer.Option(None, "--admin-email", help="Admin email"),
    admin_password: str = typer.Option(None, "--admin-password", help="Admin password"),
    skip_seed: bool = typer.Option(False, "--skip-seed", help="Skip seed data"),
    seed: str = typer.Option(
        "development", "--seed", help="Seed type: development, production"
    ),
):
    """Interactive first-time setup wizard."""
    console.print(
        Panel.fit(
            "[bold cyan]TensorWall Setup Wizard[/bold cyan]\n"
            "This will configure your gateway for first use.",
            border_style="cyan",
        )
    )

    # Step 1: Check database
    console.print("\n[bold]Step 1:[/bold] Checking database connection...")
    db_ok, db_msg = run_async(_check_db())
    if not db_ok:
        console.print(f"[red]Database error:[/red] {db_msg}")
        console.print(
            "\nPlease ensure PostgreSQL is running and DATABASE_URL is correct."
        )
        raise typer.Exit(1)
    console.print(f"  [green]βœ“[/green] {db_msg}")

    # Step 2: Check Redis
    console.print("\n[bold]Step 2:[/bold] Checking Redis connection...")
    redis_ok, redis_msg = run_async(_check_redis())
    if not redis_ok:
        console.print(f"[red]Redis error:[/red] {redis_msg}")
        console.print("\nPlease ensure Redis is running and REDIS_URL is correct.")
        raise typer.Exit(1)
    console.print(f"  [green]βœ“[/green] {redis_msg}")

    # Step 3: Run migrations
    console.print("\n[bold]Step 3:[/bold] Running database migrations...")
    migrate_result = run_async(_run_migrations())
    if not migrate_result["success"]:
        console.print(f"[red]Migration error:[/red] {migrate_result['error']}")
        raise typer.Exit(1)
    console.print("  [green]βœ“[/green] Migrations applied")

    # Step 4: Check if already setup
    is_setup = run_async(_check_setup_complete())
    if is_setup:
        console.print("\n[yellow]Setup already completed.[/yellow]")
        console.print("Use 'seed clean --confirm' to reset if needed.")
        raise typer.Exit(0)

    # Step 5: Create admin user
    console.print("\n[bold]Step 4:[/bold] Creating admin user...")

    if not non_interactive and not admin_email:
        admin_email = typer.prompt("  Admin email")
    elif not admin_email:
        console.print("[red]--admin-email required in non-interactive mode[/red]")
        raise typer.Exit(1)

    if not non_interactive and not admin_password:
        admin_password = typer.prompt(
            "  Password", hide_input=True, confirmation_prompt=True
        )
    elif not admin_password:
        admin_password = generate_password()
        console.print(f"  [yellow]Generated password:[/yellow] {admin_password}")

    admin_result = run_async(_create_admin(admin_email, admin_password))
    if not admin_result["success"]:
        console.print(f"[red]Error:[/red] {admin_result['error']}")
        raise typer.Exit(1)
    console.print(f"  [green]βœ“[/green] Admin created: {admin_email}")

    # Step 6: Seed data
    api_key = None
    if not skip_seed:
        console.print("\n[bold]Step 5:[/bold] Seeding initial data...")
        seed_result = run_async(_seed_data())
        if seed_result["success"]:
            api_key = seed_result.get("api_key")
            console.print("  [green]βœ“[/green] Data seeded")
        else:
            console.print(
                f"  [yellow]Warning:[/yellow] {seed_result.get('error', 'Seed skipped')}"
            )

    # Step 7: Mark setup complete
    run_async(_mark_setup_complete())

    # Summary
    console.print("\n")
    console.print(
        Panel.fit(
            "[bold green]Setup Complete![/bold green]",
            border_style="green",
        )
    )

    table = Table(show_header=False, box=None)
    table.add_column("Key", style="cyan")
    table.add_column("Value")
    table.add_row("Admin Email", admin_email)
    table.add_row("Dashboard", "http://localhost:3000")
    table.add_row("API", "http://localhost:8000")
    table.add_row("API Docs", "http://localhost:8000/docs")
    if api_key:
        table.add_row("API Key", api_key)

    console.print(table)

    if api_key:
        console.print(
            "\n[yellow]⚠ Save the API key now. It cannot be recovered.[/yellow]"
        )


@app.command()
def status():
    """Check setup status."""
    console.print("[cyan]Checking setup status...[/cyan]\n")

    table = Table(title="Setup Status")
    table.add_column("Check", style="cyan")
    table.add_column("Status")
    table.add_column("Details")

    # Database
    db_ok, db_msg = run_async(_check_db())
    table.add_row(
        "Database",
        "[green]OK[/green]" if db_ok else "[red]FAILED[/red]",
        db_msg,
    )

    # Redis
    redis_ok, redis_msg = run_async(_check_redis())
    table.add_row(
        "Redis",
        "[green]OK[/green]" if redis_ok else "[red]FAILED[/red]",
        redis_msg,
    )

    # Migrations
    if db_ok:
        migration_current = run_async(_get_migration_status())
        table.add_row(
            "Migrations",
            "[green]OK[/green]" if migration_current else "[yellow]Pending[/yellow]",
            migration_current or "Run 'migrate upgrade'",
        )

    # Setup complete
    if db_ok:
        is_setup = run_async(_check_setup_complete())
        table.add_row(
            "Setup Complete",
            "[green]Yes[/green]" if is_setup else "[yellow]No[/yellow]",
            "" if is_setup else "Run 'setup wizard'",
        )

    console.print(table)


async def _check_db() -> tuple[bool, str]:
    """Check database connectivity."""
    try:
        from backend.db.session import engine
        from sqlalchemy import text

        async with engine.connect() as conn:
            await conn.execute(text("SELECT version()"))
            return True, "PostgreSQL connected"
    except Exception as e:
        return False, str(e)


async def _check_redis() -> tuple[bool, str]:
    """Check Redis connectivity."""
    try:
        import redis.asyncio as redis
        from backend.core.config import settings

        client = redis.from_url(settings.redis_url)
        await client.ping()
        await client.close()
        return True, "Redis connected"
    except Exception as e:
        return False, str(e)


async def _run_migrations() -> dict:
    """Run database migrations using Alembic."""
    import os
    import subprocess

    try:
        # Find alembic.ini - could be in backend/ or project root
        backend_dir = os.path.dirname(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        )

        # Try backend/alembic.ini first, then project root
        ini_path = os.path.join(backend_dir, "alembic.ini")
        if not os.path.exists(ini_path):
            project_root = os.path.dirname(backend_dir)
            ini_path = os.path.join(project_root, "alembic.ini")

        if os.path.exists(ini_path):
            # Use Alembic via subprocess to handle async properly
            result = subprocess.run(
                ["python", "-m", "alembic", "-c", ini_path, "upgrade", "head"],
                capture_output=True,
                text=True,
                cwd=os.path.dirname(ini_path),
            )

            if result.returncode == 0:
                return {"success": True, "method": "alembic"}
            else:
                # If Alembic fails, fall back to create_all
                console.print(
                    f"  [yellow]Alembic warning:[/yellow] {result.stderr[:200] if result.stderr else 'Unknown error'}"
                )
                console.print("  [yellow]Falling back to create_all...[/yellow]")

        # Fallback: use create_all if Alembic not configured or failed
        from backend.db.session import engine
        from backend.db.models import Base

        async with engine.begin() as conn:
            await conn.run_sync(Base.metadata.create_all)

        return {"success": True, "method": "create_all"}
    except Exception as e:
        return {"success": False, "error": str(e)}


async def _check_setup_complete() -> bool:
    """Check if setup has been completed."""
    try:
        from sqlalchemy import select
        from backend.db.session import AsyncSessionLocal
        from backend.db.models import SetupState

        async with AsyncSessionLocal() as session:
            result = await session.execute(
                select(SetupState).where(SetupState.key == "setup_completed")
            )
            row = result.scalar_one_or_none()
            return row is not None and row.value == "true"
    except Exception:
        return False


async def _get_migration_status() -> str:
    """Get current migration status."""
    try:
        from sqlalchemy import text
        from backend.db.session import AsyncSessionLocal

        async with AsyncSessionLocal() as session:
            result = await session.execute(
                text("SELECT version_num FROM alembic_version LIMIT 1")
            )
            row = result.first()
            return row[0] if row else None
    except Exception:
        return None


async def _create_admin(email: str, password: str) -> dict:
    """Create admin user."""
    try:
        from datetime import datetime
        from passlib.context import CryptContext
        from backend.db.session import AsyncSessionLocal
        from backend.db.models import User, UserRole

        pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

        async with AsyncSessionLocal() as session:
            user = User(
                email=email,
                name=email.split("@")[0].title(),
                password_hash=pwd_context.hash(password),
                role=UserRole.ADMIN,
                is_active=True,
                created_at=datetime.utcnow(),
                updated_at=datetime.utcnow(),
            )
            session.add(user)
            await session.commit()

            return {"success": True}
    except Exception as e:
        return {"success": False, "error": str(e)}


async def _seed_data() -> dict:
    """Seed initial data."""
    from datetime import datetime

    try:
        from sqlalchemy import select
        from backend.db.session import AsyncSessionLocal
        from backend.db.models import (
            Application,
            ApiKey,
            Budget,
            Organization,
            Environment,
            BudgetPeriod,
            TenantStatus,
            TenantTier,
        )
        from backend.core.auth import hash_api_key

        async with AsyncSessionLocal() as session:
            # Check if already seeded
            result = await session.execute(select(Application).limit(1))
            if result.scalar_one_or_none():
                return {"success": True, "api_key": None}

            # Generate API key
            api_key_raw = f"gw_{secrets.token_urlsafe(24)}"

            # Organization
            org = Organization(
                org_id="org_default",
                name="Default Organization",
                slug="default-org",
                status=TenantStatus.ACTIVE,
                tier=TenantTier.PROFESSIONAL,
                owner_email="admin@localhost",
                created_at=datetime.utcnow(),
            )
            session.add(org)
            await session.flush()

            # Application
            app = Application(
                app_id="default-app",
                name="Default Application",
                owner="admin",
                description="Default application",
                is_active=True,
                allowed_providers=["openai", "anthropic", "ollama"],
                allowed_models=["gpt-4o-mini", "gpt-4o", "claude-3-5-sonnet-20241022"],
                created_at=datetime.utcnow(),
            )
            session.add(app)
            await session.flush()

            # API Key
            api_key = ApiKey(
                key_hash=hash_api_key(api_key_raw),
                key_prefix=api_key_raw[:12],
                name="Default API Key",
                application_id=app.id,
                environment=Environment.DEVELOPMENT,
                is_active=True,
                created_at=datetime.utcnow(),
            )
            session.add(api_key)

            # Budget
            budget = Budget(
                application_id=app.id,
                soft_limit_usd=800.0,
                hard_limit_usd=1000.0,
                period=BudgetPeriod.MONTHLY,
                is_active=True,
                created_at=datetime.utcnow(),
            )
            session.add(budget)

            await session.commit()

            return {"success": True, "api_key": api_key_raw}

    except Exception as e:
        return {"success": False, "error": str(e)}


async def _mark_setup_complete() -> None:
    """Mark setup as complete in setup_state table."""
    try:
        from datetime import datetime
        from backend.db.session import AsyncSessionLocal
        from backend.db.models import SetupState

        async with AsyncSessionLocal() as session:
            setup_state = SetupState(
                key="setup_completed",
                value="true",
                created_at=datetime.utcnow(),
                updated_at=datetime.utcnow(),
            )
            session.add(setup_state)
            await session.commit()
    except Exception:
        pass  # Best effort