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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
"""
CLI Layer for Interactive Todo Application (v2.0.0)

Owner: Menu/UX Agent
Purpose: Interactive terminal interface using Typer and Rich
Architecture: Command-driven CLI with async service orchestration

Constitutional Constraints:
- ALLOWED: Rich UI output, Typer commands, User input collection, Service layer orchestration
- FORBIDDEN: SQL queries, Direct database access, Business logic validation (delegates to services)
"""

import asyncio
import typer
import questionary
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich import print as rprint
from datetime import datetime
from typing import Optional, List, Any
from enum import Enum

from src.models import Priority, TaskStatus, TaskCreate, TaskUpdate, RecurrenceInterval
from src import services

# Initialize Typer app and Rich console
app = typer.Typer(help="Interactive Todo Application CLI")
console = Console()

class SortOption(str, Enum):
    DATE = "date"
    ALPHA = "alpha"
    PRIORITY = "priority"

def get_priority_color(priority: Any) -> str:
    """Return color associated with priority level."""
    # Handle both Enum and raw string
    val = priority.value if hasattr(priority, 'value') else str(priority)
    colors = {
        "high": "red",
        "medium": "yellow",
        "low": "green"
    }
    return colors.get(val.lower(), "white")

def display_error(message: str):
    """Display error message in a Rich panel."""
    console.print(Panel(f"[bold red]Error:[/bold red] {message}", border_style="red"))

def display_success(message: str):
    """Display success message."""
    console.print(f"[bold green]βœ“[/bold green] {message}")

async def run_with_progress(coro, description: str):
    """Run an async coroutine with a Rich progress spinner."""
    with Progress(
        SpinnerColumn(),
        TextColumn("[progress.description]{task.description}"),
        transient=True,
    ) as progress:
        progress.add_task(description=description, total=None)
        return await coro

async def interactive_date_picker(prompt: str) -> Optional[datetime]:
    """Helper to pick a date using natural language or interactive prompt."""
    date_str = await asyncio.to_thread(questionary.text(f"{prompt} (e.g. 'tomorrow', 'next mon at 3pm')").ask)
    if not date_str:
        return None

    parsed_date = services.parse_natural_date(date_str)
    if not parsed_date:
        console.print("[yellow]Could not parse date. Please use YYYY-MM-DD format or try again.[/yellow]")
        # Fallback to manual entry
        manual_str = await asyncio.to_thread(questionary.text("Due Date (YYYY-MM-DD HH:MM:SS)").ask)
        if not manual_str: return None
        try:
            return datetime.fromisoformat(manual_str.replace(' ', 'T'))
        except ValueError:
            console.print("[red]Invalid date format.[/red]")
            return None
    return parsed_date

@app.command()
def add(
    title: Optional[str] = typer.Argument(None, help="The title of the task"),
    description: Optional[str] = typer.Option(None, "--desc", "-d", help="Task description"),
    priority: Priority = typer.Option(Priority.MEDIUM, "--priority", "-p", help="Task priority"),
    category: Optional[str] = typer.Option(None, "--category", "-c", help="Task category"),
    due: Optional[str] = typer.Option(None, "--due", help="Due date (natural language)"),
    recurrence: RecurrenceInterval = typer.Option(RecurrenceInterval.NONE, "--recurrence", "-r", help="Recurrence interval"),
    interactive: bool = typer.Option(False, "--interactive", "-i", help="Use interactive wizard"),
):
    """Add a new task with optional metadata."""
    async def _add():
        nonlocal title, description, priority, category, due, recurrence

        if interactive or not title:
            # Use questionary for interactive form
            title = await asyncio.to_thread(questionary.text("Task title:", validate=lambda text: len(text) > 0).ask)
            description = await asyncio.to_thread(questionary.text("Description (optional):").ask)
            choice = await asyncio.to_thread(questionary.select(
                "Priority:",
                choices=["low", "medium", "high"],
                default=priority.value if priority else "medium"
            ).ask)
            priority = Priority(choice)
            category = await asyncio.to_thread(questionary.text("Category (optional):").ask)

            # Date picking
            due_date = await interactive_date_picker("Due Date")

            # Recurrence
            choice = await asyncio.to_thread(questionary.select(
                "Recurrence:",
                choices=["none", "daily", "weekly", "monthly"],
                default=recurrence.value if recurrence else "none"
            ).ask)
            recurrence = RecurrenceInterval(choice)
        else:
            # Command line mode
            due_date = services.parse_natural_date(due) if due else None

        task_data = TaskCreate(
            title=title,
            description=description,
            priority=priority,
            category=category,
            due_date=due_date,
            recurrence_interval=recurrence
        )
        result = await run_with_progress(services.create_task(task_data), "Creating task...")

        if result["success"]:
            display_success(result["message"])
        else:
            display_error(result["message"])

    asyncio.run(_add())

async def async_add(title: str = "", description: Optional[str] = None, priority: Priority = Priority.MEDIUM,
                    category: Optional[str] = None, due: Optional[str] = None,
                    recurrence: RecurrenceInterval = RecurrenceInterval.NONE,
                    interactive: bool = False):
    """Async version of add for interactive mode."""
    if interactive or not title:
        # Use questionary for interactive form
        title = await asyncio.to_thread(questionary.text("Task title:", validate=lambda text: len(text) > 0).ask)
        description = await asyncio.to_thread(questionary.text("Description (optional):").ask)
        choice = await asyncio.to_thread(questionary.select(
            "Priority:",
            choices=["low", "medium", "high"],
            default=priority.value if priority else "medium"
        ).ask)
        priority = Priority(choice)
        category = await asyncio.to_thread(questionary.text("Category (optional):").ask)

        # Date picking
        due_date = await interactive_date_picker("Due Date")

        # Recurrence
        choice = await asyncio.to_thread(questionary.select(
            "Recurrence:",
            choices=["none", "daily", "weekly", "monthly"],
            default=recurrence.value if recurrence else "none"
        ).ask)
        recurrence = RecurrenceInterval(choice)
    else:
        # Command line mode
        due_date = services.parse_natural_date(due) if due else None

    task_data = TaskCreate(
        title=title,
        description=description,
        priority=priority,
        category=category,
        due_date=due_date,
        recurrence_interval=recurrence
    )
    result = await run_with_progress(services.create_task(task_data), "Creating task...")

    if result["success"]:
        display_success(result["message"])
    else:
        display_error(result["message"])

async def async_list_tasks(status: Optional[TaskStatus] = None, category: Optional[str] = None,
                           priority: Optional[Priority] = None, sort: str = "date"):
    """Async version of list_tasks for interactive mode."""
    sort_enum = SortOption(sort) if sort in ["date", "alpha", "priority"] else SortOption.DATE

    result = await run_with_progress(
        services.get_tasks(status=status, category=category, priority=priority, sort_by=sort_enum.value),
        "Fetching tasks..."
    )

    if not result["success"]:
        display_error(result["message"])
        return

    tasks = result["data"]
    if not tasks:
        console.print("[yellow]No tasks found matching criteria.[/yellow]")
        return

    table = Table(title="Todo Tasks", header_style="bold cyan")
    table.add_column("ID", justify="right", style="cyan", no_wrap=True)
    table.add_column("Status", justify="center")
    table.add_column("Priority", justify="center")
    table.add_column("Title", style="white")
    table.add_column("Category", style="blue")
    table.add_column("Due Date", style="green")
    table.add_column("Recur", style="yellow")

    for task in tasks:
        # Normalize status to icon
        task_status = task.status.value if hasattr(task.status, 'value') else str(task.status)
        status_icon = "[green]βœ“[/green]" if task_status == "completed" else "[yellow]β—‹[/yellow]"

        # Normalize priority and recurrence
        p_val = task.priority.value if hasattr(task.priority, 'value') else str(task.priority)
        r_val = task.recurrence_interval.value if hasattr(task.recurrence_interval, 'value') else str(task.recurrence_interval)

        p_color = get_priority_color(p_val)
        priority_str = f"[{p_color}]{p_val.upper()}[/{p_color}]"
        due_str = task.due_date.strftime("%Y-%m-%d %H:%M") if task.due_date else "-"
        recur_str = r_val if r_val != "none" else "-"

        table.add_row(
            str(task.id),
            status_icon,
            priority_str,
            task.title,
            task.category or "-",
            due_str,
            recur_str
        )

    console.print(table)
    console.print(f"\n[italic]{result['message']}[/italic]")

async def async_search(query: str):
    """Async version of search for interactive mode."""
    result = await run_with_progress(services.search_tasks(query), f"Searching for '{query}'...")

    if not result["success"]:
        display_error(result["message"])
        return

    tasks = result["data"]
    if not tasks:
        console.print(f"[yellow]No tasks found matching '{query}'.[/yellow]")
        return

    table = Table(title=f"Search Results: {query}")
    table.add_column("ID", justify="right", style="cyan")
    table.add_column("Status", justify="center")
    table.add_column("Title", style="magenta")

    for task in tasks:
        status_icon = "βœ“" if task.status == TaskStatus.COMPLETED else "β—‹"
        table.add_row(str(task.id), status_icon, task.title)

    console.print(table)

async def async_update(task_id: int, title: Optional[str] = None, description: Optional[str] = None,
                      priority: Optional[Priority] = None, category: Optional[str] = None,
                      due: Optional[str] = None, status: Optional[TaskStatus] = None,
                      recurrence: Optional[RecurrenceInterval] = None,
                      interactive: bool = False):
    """Async version of update for interactive mode."""
    if interactive:
        # Fetch existing task first
        res = await services.get_tasks()
        existing = None
        for t in res["data"]:
            if t.id == task_id:
                existing = t
                break

        if existing:
            title = await asyncio.to_thread(questionary.text("New title:", default=existing.title).ask)
            choice = await asyncio.to_thread(questionary.select(
                "New Priority:",
                choices=["low", "medium", "high"],
                default=existing.priority if existing.priority else "medium"
            ).ask)
            priority = Priority(choice)
            due_date = await interactive_date_picker("New Due Date")
            choice = await asyncio.to_thread(questionary.select(
                "New Recurrence:",
                choices=["none", "daily", "weekly", "monthly"],
                default=existing.recurrence_interval if existing.recurrence_interval else "none"
            ).ask)
            recurrence = RecurrenceInterval(choice)
        else:
            display_error(f"Task {task_id} not found.")
            return
    else:
        # Command line mode
        due_date = services.parse_natural_date(due) if due else None

    # Handle completion status specifically for boolean transition
    is_completed = None
    if status:
        is_completed = (status == TaskStatus.COMPLETED)

    update_data = TaskUpdate(
        title=title,
        description=description,
        priority=priority,
        category=category,
        due_date=due_date,
        is_completed=is_completed,
        recurrence_interval=recurrence
    )

    result = await run_with_progress(services.update_task(task_id, update_data), f"Updating task {task_id}...")

    if result["success"]:
        display_success(result["message"])
    else:
        display_error(result["message"])

async def async_delete(task_id: int, force: bool = True):
    """Async version of delete for interactive mode."""
    result = await run_with_progress(services.delete_task(task_id), f"Deleting task {task_id}...")

    if result["success"]:
        display_success(result["message"])
    else:
        display_error(result["message"])

async def async_complete(task_id: int):
    """Async version of complete for interactive mode."""
    result = await run_with_progress(services.toggle_task_completion(task_id), f"Toggling status for task {task_id}...")

    if result["success"]:
        display_success(result["message"])
    else:
        display_error(result["message"])

async def async_history(root_task_id: int):
    """Async version of history for interactive mode."""
    result = await run_with_progress(services.get_task_history(root_task_id), f"Fetching history for {root_task_id}...")

    if not result["success"]:
        display_error(result["message"])
        return

    tasks = result["data"]
    table = Table(title=f"Task History Series: {root_task_id}", header_style="bold cyan")
    table.add_column("ID", justify="right")
    table.add_column("Status")
    table.add_column("Completed Date")
    table.add_column("Due Date")

    for task in tasks:
        status_icon = "βœ“" if task.status == TaskStatus.COMPLETED else "β—‹"
        completed_str = task.updated_at.strftime("%Y-%m-%d %H:%M") if task.status == TaskStatus.COMPLETED else "-"
        due_str = task.due_date.strftime("%Y-%m-%d %H:%M") if task.due_date else "-"
        table.add_row(str(task.id), status_icon, completed_str, due_str)

    console.print(table)

@app.command(name="list")
def list_tasks(
    status: Optional[TaskStatus] = typer.Option(None, "--status", help="Filter by status"),
    category: Optional[str] = typer.Option(None, "--category", help="Filter by category"),
    priority: Optional[Priority] = typer.Option(None, "--priority", help="Filter by priority"),
    sort: SortOption = typer.Option(SortOption.DATE, "--sort", help="Sort order"),
):
    """List tasks with optional filtering and sorting."""
    async def _list():
        await async_list_tasks(status=status, category=category, priority=priority, sort=sort.value)

    asyncio.run(_list())

@app.command()
def search(
    query: str = typer.Argument(..., help="Search keyword"),
):
    """Search for tasks by title or description."""
    async def _search():
        await async_search(query)

    asyncio.run(_search())

@app.command()
def update(
    task_id: int = typer.Argument(..., help="ID of the task to update"),
    title: Optional[str] = typer.Option(None, "--title", help="New title"),
    description: Optional[str] = typer.Option(None, "--desc", help="New description"),
    priority: Optional[Priority] = typer.Option(None, "--priority", help="New priority"),
    category: Optional[str] = typer.Option(None, "--category", help="New category"),
    due: Optional[str] = typer.Option(None, "--due", help="New due date (natural language)"),
    status: Optional[TaskStatus] = typer.Option(None, "--status", help="New status"),
    recurrence: Optional[RecurrenceInterval] = typer.Option(None, "--recurrence", help="New recurrence interval"),
    interactive: bool = typer.Option(False, "--interactive", "-i", help="Use interactive wizard"),
):
    """Update task metadata."""
    async def _update():
        nonlocal title, description, priority, category, due, status, recurrence

        if interactive:
            # Fetch existing task first
            res = await services.get_tasks() # Inefficient but simple for now
            existing = None
            for t in res["data"]:
                if t.id == task_id:
                    existing = t
                    break

            if existing:
                title = await asyncio.to_thread(questionary.text("New title:", default=existing.title).ask)
                priority = Priority(await asyncio.to_thread(questionary.select("Priority:", choices=["low", "medium", "high"], default=existing.priority).ask))
                due_date = await interactive_date_picker("New Due Date")
                recurrence = RecurrenceInterval(await asyncio.to_thread(questionary.select("Recurrence:", choices=["none", "daily", "weekly", "monthly"], default=existing.recurrence_interval).ask))
            else:
                display_error(f"Task {task_id} not found.")
                return
        else:
            due_date = services.parse_natural_date(due) if due else None

        # Convert status to bool for repository
        is_completed = None
        if status:
            is_completed = (status == TaskStatus.COMPLETED)

        update_data = TaskUpdate(
            title=title,
            description=description,
            priority=priority,
            category=category,
            due_date=due_date,
            is_completed=is_completed,
            recurrence_interval=recurrence
        )

        result = await run_with_progress(services.update_task(task_id, update_data), f"Updating task {task_id}...")

        if result["success"]:
            display_success(result["message"])
        else:
            display_error(result["message"])

    asyncio.run(_update())

@app.command()
def delete(
    task_id: int = typer.Argument(..., help="ID of the task to delete"),
    force: bool = typer.Option(False, "--force", "-f", help="Skip confirmation"),
):
    """Delete a task."""
    async def _delete():
        await async_delete(task_id, force)

    asyncio.run(_delete())

@app.command()
def complete(
    task_id: int = typer.Argument(..., help="ID of the task to toggle completion"),
):
    """Toggle task completion status."""
    async def _toggle():
        await async_complete(task_id)

    asyncio.run(_toggle())

@app.command()
def history(
    root_task_id: int = typer.Argument(..., help="ID of the original task"),
):
    """View history of a recurring task series."""
    async def _history():
        result = await run_with_progress(services.get_task_history(root_task_id), f"Fetching history for {root_task_id}...")

        if not result["success"]:
            display_error(result["message"])
            return

        tasks = result["data"]
        table = Table(title=f"Task History Series: {root_task_id}", header_style="bold cyan")
        table.add_column("ID", justify="right")
        table.add_column("Status")
        table.add_column("Completed Date")
        table.add_column("Due Date")

        for task in tasks:
            status_icon = "βœ“" if task.status == TaskStatus.COMPLETED else "β—‹"
            completed_str = task.updated_at.strftime("%Y-%m-%d %H:%M") if task.status == TaskStatus.COMPLETED else "-"
            due_str = task.due_date.strftime("%Y-%m-%d %H:%M") if task.due_date else "-"
            table.add_row(str(task.id), status_icon, completed_str, due_str)

        console.print(table)

    asyncio.run(_history())

if __name__ == "__main__":
    app()