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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
698b2c1
35765b5
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
"""FastAPI application for Project Memory - API layer calling MCP tools."""

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from contextlib import asynccontextmanager
from typing import List, Optional
import os
import asyncio
from dotenv import load_dotenv

from app import schemas
from app.schemas import (
    ProjectCreate, ProjectJoin, Project,
    TaskCreate, Task, TaskCompleteRequest, TaskCompleteResponse,
    ActivityResponse, SearchRequest, SearchResponse,
    SmartQueryRequest, SmartQueryResponse,
    ChatRequest, ChatResponse, ErrorResponse,
    UserCreate, User
)

from app.tools.projects import create_project, list_projects, join_project, check_project_id_available
from app.tools.tasks import create_task, list_tasks, list_activity
from app.tools.memory import complete_task, memory_search

# Load environment variables
load_dotenv()


@asynccontextmanager
async def lifespan(app: FastAPI):
    """Initialize database and vector store on startup."""
    from app.database import init_db, SessionLocal
    from app.vectorstore import init_vectorstore
    from app.models import User, AI_AGENT_USER_ID
    from app.agent_worker import agent_loop

    init_db()
    init_vectorstore()
    print("[OK] Database and vector store initialized")

    # Ensure AI Agent user exists
    db = SessionLocal()
    try:
        if not db.query(User).filter(User.id == AI_AGENT_USER_ID).first():
            db.add(User(id=AI_AGENT_USER_ID, first_name="AI", last_name="Agent"))
            db.commit()
            print("[OK] AI Agent user created")
    finally:
        db.close()

    # Start agent worker in background
    agent_task = asyncio.create_task(agent_loop())
    print("[OK] Agent worker started")

    yield

    # Cleanup on shutdown
    agent_task.cancel()
    try:
        await agent_task
    except asyncio.CancelledError:
        pass
    print("[OK] Agent worker stopped")


# Initialize FastAPI app
app = FastAPI(
    title="Project Memory API",
    description="Multi-user, multi-project AI memory system powered by MCP",
    version="1.0.0",
    lifespan=lifespan
)

# Configure CORS
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:5173")
app.add_middleware(
    CORSMiddleware,
    allow_origins=[frontend_url, "http://localhost:5173", "http://localhost:3000"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# ==================== Health Check ====================
@app.get("/")
async def root():
    """Health check endpoint."""
    return {"status": "ok", "message": "Project Memory API is running"}


# ==================== User Endpoints ====================
@app.post("/api/users", response_model=schemas.User)
async def create_user(user: schemas.UserCreate):
    """Create a new user."""
    from app.database import get_db
    from app.models import User, generate_user_id
    
    db = next(get_db())
    
    # Generate unique user ID (first 3 letters of firstname + 4 random digits)
    user_id = generate_user_id(user.firstName)
    
    # Ensure ID is unique (regenerate if collision)
    while db.query(User).filter(User.id == user_id).first():
        user_id = generate_user_id(user.firstName)
    
    # Create new user
    new_user = User(
        id=user_id,
        first_name=user.firstName,
        last_name=user.lastName,
        avatar_url=user.avatar_url
    )
    db.add(new_user)
    db.commit()
    db.refresh(new_user)
    
    return {
        "id": new_user.id,
        "firstName": new_user.first_name,
        "lastName": new_user.last_name,
        "avatar_url": new_user.avatar_url,
        "created_at": new_user.created_at
    }


@app.get("/api/users/{user_id}", response_model=schemas.User)
async def get_user(user_id: str):
    """Get user by ID."""
    from app.database import get_db
    from app.models import User
    
    db = next(get_db())
    user = db.query(User).filter(User.id == user_id).first()
    
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    
    return {
        "id": user.id,
        "firstName": user.first_name,
        "lastName": user.last_name,
        "avatar_url": user.avatar_url,
        "created_at": user.created_at
    }


@app.get("/api/users")
async def list_users():
    """List all users."""
    from app.database import get_db
    from app.models import User
    
    db = next(get_db())
    users = db.query(User).all()
    return [{"id": u.id, "firstName": u.first_name, "lastName": u.last_name} for u in users]


# ==================== Project Endpoints ====================
@app.get("/api/projects/check/{project_id}")
async def check_project_availability(project_id: str):
    """Check if a project ID is available."""
    try:
        result = check_project_id_available(project_id=project_id)
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/api/projects")
async def get_projects(userId: str):
    """List all projects for a user."""
    try:
        result = list_projects(user_id=userId)
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/api/projects", response_model=Project)
async def create_new_project(project: ProjectCreate):
    """Create a new project."""
    try:
        result = create_project(
            name=project.name,
            description=project.description,
            user_id=project.userId
        )
        if "error" in result:
            raise HTTPException(status_code=400, detail=result["error"])
        return result
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/api/projects/{project_id}/join")
async def join_existing_project(project_id: str, request: ProjectJoin):
    """Join an existing project."""
    try:
        result = join_project(
            project_id=project_id,
            user_id=request.userId
        )
        if "error" in result:
            raise HTTPException(status_code=400, detail=result["error"])
        return result
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/api/projects/{project_id}/members")
async def get_project_members(project_id: str):
    """Get all members of a project."""
    from app.database import get_db
    from app.models import Project, ProjectMembership, User

    db = next(get_db())
    try:
        # Check project exists
        project = db.query(Project).filter(Project.id == project_id).first()
        if not project:
            raise HTTPException(status_code=404, detail="Project not found")

        # Get all memberships with user details
        memberships = db.query(ProjectMembership, User).join(
            User, ProjectMembership.user_id == User.id
        ).filter(ProjectMembership.project_id == project_id).all()

        members = [
            {
                "id": user.id,
                "firstName": user.first_name,
                "lastName": user.last_name,
                "avatar_url": user.avatar_url,
                "role": membership.role,
                "joined_at": membership.joined_at.isoformat() if membership.joined_at else None
            }
            for membership, user in memberships
        ]

        return {"members": members}
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        db.close()


# ==================== Agent Endpoints ====================
@app.post("/api/projects/{project_id}/agent/enable")
async def enable_agent(project_id: str):
    """Enable AI agent for this project. Adds agent to team."""
    from app.database import get_db
    from app.models import Project, ProjectMembership, AI_AGENT_USER_ID

    db = next(get_db())
    try:
        # Check project exists
        project = db.query(Project).filter(Project.id == project_id).first()
        if not project:
            raise HTTPException(status_code=404, detail="Project not found")

        # Enable agent
        project.agent_enabled = True

        # Add agent to project membership if not already
        existing = db.query(ProjectMembership).filter(
            ProjectMembership.project_id == project_id,
            ProjectMembership.user_id == AI_AGENT_USER_ID
        ).first()

        if not existing:
            membership = ProjectMembership(
                project_id=project_id,
                user_id=AI_AGENT_USER_ID,
                role="agent"
            )
            db.add(membership)

        db.commit()
        return {"message": "AI Agent enabled", "project_id": project_id}
    except HTTPException:
        raise
    except Exception as e:
        db.rollback()
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        db.close()


@app.post("/api/projects/{project_id}/agent/disable")
async def disable_agent(project_id: str):
    """Disable AI agent for this project. Removes from team."""
    from app.database import get_db
    from app.models import Project, ProjectMembership, AI_AGENT_USER_ID

    db = next(get_db())
    try:
        # Check project exists
        project = db.query(Project).filter(Project.id == project_id).first()
        if not project:
            raise HTTPException(status_code=404, detail="Project not found")

        # Disable agent
        project.agent_enabled = False

        # Remove agent from project membership
        db.query(ProjectMembership).filter(
            ProjectMembership.project_id == project_id,
            ProjectMembership.user_id == AI_AGENT_USER_ID
        ).delete()

        db.commit()
        return {"message": "AI Agent disabled", "project_id": project_id}
    except HTTPException:
        raise
    except Exception as e:
        db.rollback()
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        db.close()


# ==================== Task Endpoints ====================
@app.get("/api/projects/{project_id}/tasks")
async def get_project_tasks(project_id: str, status: Optional[str] = None):
    """Get all tasks for a project, optionally filtered by status."""
    try:
        result = list_tasks(
            project_id=project_id,
            status=status
        )
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/api/projects/{project_id}/tasks", response_model=Task)
async def create_new_task(project_id: str, task: TaskCreate):
    """Create a new task in a project."""
    try:
        result = create_task(
            project_id=project_id,
            title=task.title,
            description=task.description,
            assigned_to=task.assignedTo
        )
        if "error" in result:
            raise HTTPException(status_code=400, detail=result["error"])
        return result
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/api/projects/{project_id}/tasks/generate")
async def generate_project_tasks(project_id: str, request: dict = None):
    """Generate demo tasks for a project using AI.

    Does NOT save to database - returns generated tasks for user to edit.
    Max 50 tasks.
    """
    from app.llm import generate_tasks
    from app.database import get_db
    from app.models import Project

    db = next(get_db())
    try:
        # Get project details
        project = db.query(Project).filter(Project.id == project_id).first()
        if not project:
            raise HTTPException(status_code=404, detail="Project not found")

        # Get count from request, default 50, max 50
        count = min(request.get("count", 50) if request else 50, 50)

        # Generate tasks using LLM (no user prompt needed)
        tasks = await generate_tasks(
            project_name=project.name,
            project_description=project.description,
            count=count
        )

        return {"tasks": tasks}
    except HTTPException:
        raise
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        db.close()


@app.post("/api/tasks/{task_id}/complete", response_model=TaskCompleteResponse)
async def complete_existing_task(task_id: str, request: TaskCompleteRequest):
    """Complete a task with documentation. Generates AI docs and stores embeddings."""
    from app.database import get_db
    from app.models import Task as TaskModel

    db = next(get_db())
    try:
        task = db.query(TaskModel).filter(TaskModel.id == task_id).first()

        if not task:
            raise HTTPException(status_code=404, detail="Task not found")

        result = await complete_task(
            task_id=task_id,
            project_id=task.project_id,
            user_id=request.userId,
            what_i_did=request.whatIDid,
            code_snippet=request.codeSnippet
        )

        if "error" in result:
            raise HTTPException(status_code=400, detail=result["error"])

        return result
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        db.close()


@app.patch("/api/tasks/{task_id}/status")
async def update_task_status(task_id: str, request: dict):
    """Update task status (for kanban board)."""
    from app.database import get_db
    from app.models import Task as TaskModel, TaskStatus

    db = next(get_db())
    try:
        task = db.query(TaskModel).filter(TaskModel.id == task_id).first()

        if not task:
            raise HTTPException(status_code=404, detail="Task not found")

        new_status = request.get("status")
        user_id = request.get("userId")  # Who is making this change

        if new_status not in ["todo", "in_progress", "done"]:
            raise HTTPException(status_code=400, detail="Invalid status. Must be: todo, in_progress, or done")

        # Update working_by based on status
        if new_status == "in_progress" and user_id:
            task.working_by = user_id
        elif new_status in ["todo", "done"]:
            task.working_by = None  # Clear when not in progress

        task.status = TaskStatus(new_status)
        db.commit()
        db.refresh(task)

        return {
            "id": str(task.id),
            "project_id": task.project_id,
            "title": task.title,
            "description": task.description,
            "status": task.status.value,
            "assigned_to": task.assigned_to,
            "working_by": task.working_by,
            "created_at": task.created_at.isoformat() if task.created_at else None
        }
    except HTTPException:
        raise
    except Exception as e:
        db.rollback()
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        db.close()


@app.post("/api/tasks/{task_id}/chat")
async def chat_with_task_agent(task_id: str, request: dict):
    """Chat with AI agent while working on a task.

    The agent can answer questions, search project memory, and complete tasks.
    """
    from app.database import get_db
    from app.models import Task as TaskModel
    from app.llm import task_chat

    db = next(get_db())
    try:
        # Get task details
        task = db.query(TaskModel).filter(TaskModel.id == task_id).first()
        if not task:
            raise HTTPException(status_code=404, detail="Task not found")

        # Extract request data
        project_id = request.get("projectId", task.project_id)
        user_id = request.get("userId")
        message = request.get("message")
        history = request.get("history", [])
        current_datetime = request.get("currentDatetime", "")

        if not user_id:
            raise HTTPException(status_code=400, detail="userId is required")
        if not message:
            raise HTTPException(status_code=400, detail="message is required")

        # Call the task chat function
        result = await task_chat(
            task_id=task_id,
            task_title=task.title,
            task_description=task.description or "",
            project_id=project_id,
            user_id=user_id,
            message=message,
            history=history,
            current_datetime=current_datetime
        )

        return result
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        db.close()


# ==================== Activity Feed Endpoint ====================
@app.get("/api/projects/{project_id}/activity")
async def get_project_activity(project_id: str, limit: int = 20):
    """Get recent activity for a project."""
    try:
        result = list_activity(
            project_id=project_id,
            limit=limit
        )
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


# ==================== Search Endpoint ====================
@app.post("/api/projects/{project_id}/search", response_model=SearchResponse)
async def search_project_memory(project_id: str, request: SearchRequest):
    """Semantic search across project memory."""
    try:
        result = await memory_search(
            project_id=project_id,
            query=request.query,
            filters=request.filters.dict() if request.filters else None
        )
        
        if "error" in result:
            raise HTTPException(status_code=400, detail=result["error"])
        
        return result
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


# ==================== Smart Query Endpoint ====================
@app.post("/api/projects/{project_id}/smart-query", response_model=SmartQueryResponse)
async def smart_query_project(project_id: str, request: SmartQueryRequest):
    """Natural language query with context awareness.

    Understands queries like:
    - "What did I do yesterday?"
    - "What did Alice do today?"
    - "How does the auth system work?"
    - "Task 13 status?"
    """
    try:
        from app.smart_query import smart_query

        result = await smart_query(
            project_id=project_id,
            query=request.query,
            current_user_id=request.currentUserId,
            current_datetime=request.currentDatetime
        )

        if "error" in result.get("answer", ""):
            raise HTTPException(status_code=400, detail=result["answer"])

        return result
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


# ==================== Chat Endpoint ====================
@app.post("/api/chat", response_model=ChatResponse)
async def chat_with_ai(request: ChatRequest):
    """Chat with AI using MCP tools."""
    try:
        # Import here to avoid circular dependency
        from app.llm import chat_with_tools
        
        # Convert messages to dict format
        messages = [{"role": msg.role, "content": msg.content} for msg in request.messages]
        
        # Call the chat function with tool support
        result = await chat_with_tools(
            messages=messages,
            project_id=request.projectId
        )
        
        return {"message": result}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


# ==================== Error Handlers ====================
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
    """Custom HTTP exception handler."""
    return JSONResponse(
        status_code=exc.status_code,
        content={
            "error": exc.detail,
            "status_code": exc.status_code
        }
    )


@app.exception_handler(Exception)
async def general_exception_handler(request, exc):
    """General exception handler."""
    return JSONResponse(
        status_code=500,
        content={
            "error": "Internal server error",
            "detail": str(exc)
        }
    )


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "app.main:app",
        host="0.0.0.0",
        port=8000,
        reload=True
    )