File size: 12,020 Bytes
c6253b2
 
 
 
 
 
 
fb29daa
9af2b22
c6253b2
 
 
fb29daa
9af2b22
c6253b2
fb29daa
 
 
 
 
 
c6253b2
 
 
9af2b22
 
fb29daa
 
 
 
 
 
 
 
c6253b2
 
 
 
 
 
 
 
 
 
9af2b22
c6253b2
 
 
 
 
 
 
9af2b22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6253b2
9af2b22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6253b2
 
fb29daa
 
 
 
 
 
 
 
 
c6253b2
 
9af2b22
c6253b2
 
 
 
 
 
 
9af2b22
c6253b2
 
 
 
 
 
 
 
9af2b22
c6253b2
 
 
 
 
 
 
 
 
 
 
 
 
9af2b22
c6253b2
 
 
 
 
 
 
 
 
 
9af2b22
c6253b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9af2b22
c6253b2
 
 
 
 
 
 
fb29daa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9af2b22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6253b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9af2b22
c6253b2
 
 
 
 
9af2b22
 
c6253b2
 
9af2b22
 
 
c6253b2
 
9af2b22
 
 
 
 
 
 
c6253b2
 
 
 
 
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
"""Human schedule/plan routes: CRUD, feedback, priors, templates, reschedule.

Session-authenticated; mirrors the app envelope pattern.
"""

from __future__ import annotations

import json
from datetime import date
from typing import Any

from fastapi import APIRouter, Depends, Request, status
from fastapi.responses import Response
from pydantic import BaseModel

from app.chat_plan import (
    ChatPlanError,
    chat_plan_to_day_put,
    plan_and_feedback_to_chat_plan,
    unwrap_import_payload,
)
from app.deps import require_login
from app.models import ApiEnvelope, err, ok
from app.schedule_math import capacity_hint, trigger_operators
from app.schedule_reschedule import seed_starter_blocks
from app.schedule_risk import risk_from_stores
from app.schedule_store import (
    BlockCheckBody,
    BlockCreate,
    BlockFeedback,
    BlockPatch,
    DayPlanPut,
    PlanMetaPatch,
)

router = APIRouter(tags=["plan"], dependencies=[Depends(require_login)])


class RescheduleBody(BaseModel):
    reason: str = "user"
    force: bool = False


class FeedbackBody(BlockFeedback):
    block_id: str = ""
    date: str | None = None


def _store(request: Request):
    return request.app.state.schedule_store


def _risk_bundle(request: Request, target: date | None = None) -> dict[str, Any]:
    risk = risk_from_stores(
        request.app.state.entry_store,
        request.app.state.daily_store,
        target_day=target,
    )
    cap = capacity_hint(
        risk_1h_score=risk["risk_1h_score"],
        triggers_yesterday=risk["triggers_yesterday"],
        yesterday_trigger=risk["yesterday_trigger"],
        last_hour_high=risk["last_hour_high"],
    )
    ops = trigger_operators(
        risk_1h_tags=risk["risk_1h_tags"],
        triggers_yesterday=risk["triggers_yesterday"],
    )
    return {**risk, "capacity_hint": cap, "operators": ops}


def _enrich_plan(request: Request, day: date) -> dict[str, Any]:
    data = _store(request).plan_with_feedback(day.isoformat())
    bundle = _risk_bundle(request, day)
    data["capacity_hint"] = bundle["capacity_hint"]
    data["operators"] = bundle["operators"]
    data["risk_1h"] = {
        "score": bundle["risk_1h_score"],
        "tags": bundle["risk_1h_tags"],
        "high": bundle["last_hour_high"],
    }
    data["triggers_yesterday"] = bundle["triggers_yesterday"]
    return data


def _feedback_map(store: Any, day: str) -> dict[str, dict[str, Any]]:
    by_block: dict[str, dict[str, Any]] = {}
    for row in store.feedback_for_plan(day):
        bid = str(row.get("block_id") or "")
        if bid:
            by_block[bid] = row
    return by_block


@router.get("/api/plan/{day}", response_model=ApiEnvelope)
def get_plan(request: Request, day: date) -> dict[str, Any]:
    return ok(_enrich_plan(request, day))


@router.put("/api/plan/{day}", response_model=ApiEnvelope)
def put_plan(request: Request, day: date, body: DayPlanPut) -> object:
    try:
        put = body.model_copy(update={"source": body.source or "user"})
        _store(request).save_plan(day.isoformat(), put)
        return ok(_enrich_plan(request, day))
    except ValueError as exc:
        return err("validation_error", str(exc), status.HTTP_422_UNPROCESSABLE_ENTITY)


@router.post("/api/plan/{day}/blocks", response_model=ApiEnvelope)
def add_block(request: Request, day: date, body: BlockCreate) -> object:
    try:
        _store(request).add_block(day.isoformat(), body)
        return ok(_enrich_plan(request, day))
    except ValueError as exc:
        return err("validation_error", str(exc), status.HTTP_422_UNPROCESSABLE_ENTITY)


@router.patch("/api/plan/{day}/blocks/{block_id}", response_model=ApiEnvelope)
def patch_block(
    request: Request,
    day: date,
    block_id: str,
    body: BlockPatch,
) -> object:
    try:
        _store(request).patch_block(day.isoformat(), block_id, body)
        return ok(_enrich_plan(request, day))
    except KeyError:
        return err("not_found", "Block not found", status.HTTP_404_NOT_FOUND)
    except ValueError as exc:
        return err("validation_error", str(exc), status.HTTP_422_UNPROCESSABLE_ENTITY)


@router.delete("/api/plan/{day}/blocks/{block_id}", response_model=ApiEnvelope)
def delete_block(request: Request, day: date, block_id: str) -> object:
    try:
        _store(request).delete_block(day.isoformat(), block_id)
        return ok(_enrich_plan(request, day))
    except KeyError:
        return err("not_found", "Block not found", status.HTTP_404_NOT_FOUND)


@router.post("/api/plan/{day}/blocks/{block_id}/feedback", response_model=ApiEnvelope)
def post_feedback(
    request: Request,
    day: date,
    block_id: str,
    body: FeedbackBody,
) -> object:
    fb = body.model_copy(update={"block_id": block_id, "date": day.isoformat()})
    try:
        stored, _plan = _store(request).submit_feedback(day.isoformat(), fb)
        return ok(
            {
                "feedback": stored.model_dump(mode="json"),
                "plan": _enrich_plan(request, day),
                "priors": list(_store(request).load_priors().values()),
            }
        )
    except KeyError:
        return err("not_found", "Block not found", status.HTTP_404_NOT_FOUND)


@router.post("/api/plan/{day}/blocks/{block_id}/check", response_model=ApiEnvelope)
def check_block(
    request: Request,
    day: date,
    block_id: str,
    body: BlockCheckBody,
) -> object:
    """Fast checkbox — status only, no full feedback required."""

    try:
        _store(request).patch_block(
            day.isoformat(),
            block_id,
            BlockPatch(status=body.status),
        )
        if body.status == "skipped" and body.skip_reason:
            try:
                _store(request).submit_feedback(
                    day.isoformat(),
                    BlockFeedback(
                        block_id=block_id,
                        date=day.isoformat(),
                        did="skipped",
                        skip_reason=body.skip_reason,
                    ),
                )
            except KeyError:
                pass
        return ok(_enrich_plan(request, day))
    except KeyError:
        return err("not_found", "Block not found", status.HTTP_404_NOT_FOUND)
    except ValueError as exc:
        return err("validation_error", str(exc), status.HTTP_422_UNPROCESSABLE_ENTITY)


@router.patch("/api/plan/{day}/meta", response_model=ApiEnvelope)
def patch_plan_meta(request: Request, day: date, body: PlanMetaPatch) -> object:
    try:
        _store(request).patch_meta(day.isoformat(), body)
        return ok(_enrich_plan(request, day))
    except ValueError as exc:
        return err("validation_error", str(exc), status.HTTP_422_UNPROCESSABLE_ENTITY)


@router.post("/api/plan/{day}/import", response_model=ApiEnvelope)
def import_chat_plan(request: Request, day: date, body: dict[str, Any]) -> object:
    store = _store(request)
    day_s = day.isoformat()
    try:
        chat, mode = unwrap_import_payload(body)
        current = store.get_plan(day_s)
        put, feedbacks, warnings = chat_plan_to_day_put(
            chat,
            day_s,
            max_blocks=request.app.state.settings.schedule_max_blocks,
            previous_blocks=[b.model_dump() for b in current.blocks],
            mode=mode,
        )
        store.save_plan(day_s, put, bump_version=True)
        for fb in feedbacks:
            try:
                store.submit_feedback(day_s, fb)
            except KeyError:
                warnings.append(f"feedback skipped for missing block {fb.block_id}")
        data = _enrich_plan(request, day)
        data["import_warnings"] = warnings
        data["import_mode"] = mode
        return ok(data)
    except ChatPlanError as exc:
        return err("validation_error", str(exc), status.HTTP_422_UNPROCESSABLE_ENTITY)
    except ValueError as exc:
        return err("validation_error", str(exc), status.HTTP_422_UNPROCESSABLE_ENTITY)


@router.get("/api/plan/{day}/export", response_model=ApiEnvelope)
def export_chat_plan(request: Request, day: date) -> dict[str, Any]:
    store = _store(request)
    day_s = day.isoformat()
    plan = store.get_plan(day_s)
    chat = plan_and_feedback_to_chat_plan(plan, _feedback_map(store, day_s))
    return ok(chat)


@router.get("/api/plan/{day}/export.txt")
def export_chat_plan_file(request: Request, day: date) -> Response:
    store = _store(request)
    day_s = day.isoformat()
    plan = store.get_plan(day_s)
    chat = plan_and_feedback_to_chat_plan(plan, _feedback_map(store, day_s))
    body = json.dumps(chat, ensure_ascii=False, indent=2) + "\n"
    return Response(
        content=body,
        media_type="application/json",
        headers={
            "Content-Disposition": f'attachment; filename="plan-{day_s}.json"'
        },
    )


@router.post("/api/plan/{day}/seed", response_model=ApiEnvelope)
def seed_plan(request: Request, day: date) -> object:
    """Fill an empty day from templates under live capacity (P0 locked)."""

    store = _store(request)
    existing = store.get_plan(day.isoformat())
    if existing.blocks:
        return err(
            "validation_error",
            "Day already has blocks — clear or reschedule instead",
            status.HTTP_422_UNPROCESSABLE_ENTITY,
        )
    bundle = _risk_bundle(request, day)
    blocks = seed_starter_blocks(
        day.isoformat(),
        capacity_hint=bundle["capacity_hint"],
        max_blocks=request.app.state.settings.schedule_max_blocks,
    )
    try:
        store.save_plan(
            day.isoformat(),
            DayPlanPut(
                blocks=blocks,
                source="rules",
                capacity_hint=bundle["capacity_hint"],
                notes="seed",
                force_p0_move=True,
            ),
        )
        data = _enrich_plan(request, day)
        data["seeded"] = True
        return ok(data)
    except ValueError as exc:
        return err("validation_error", str(exc), status.HTTP_422_UNPROCESSABLE_ENTITY)


@router.get("/api/schedule/priors", response_model=ApiEnvelope)
def get_priors(request: Request) -> dict[str, Any]:
    kinds = _store(request).load_priors()
    return ok({"kinds": list(kinds.values()), "markdown": _store(request).priors_table()})


@router.get("/api/schedule/templates", response_model=ApiEnvelope)
def get_templates(request: Request) -> dict[str, Any]:
    return ok({"items": _store(request).templates()})


@router.get("/api/plan/{day}/health", response_model=ApiEnvelope)
def get_health(request: Request, day: date) -> dict[str, Any]:
    return ok(_store(request).health(day.isoformat()))


@router.post("/api/plan/{day}/reschedule", response_model=ApiEnvelope)
def reschedule(request: Request, day: date, body: RescheduleBody | None = None) -> object:
    payload = body or RescheduleBody()
    bundle = _risk_bundle(request, day)
    try:
        result = request.app.state.reschedule_service.reschedule(
            day.isoformat(),
            reason=payload.reason,
            force=payload.force,
            risk_score=bundle["risk_1h_score"],
            capacity_hint=bundle["capacity_hint"],
            priors_md=_store(request).priors_table(),
            context_notes=(
                f"risk_tags={bundle['risk_1h_tags']}; "
                f"triggers_y={bundle['triggers_yesterday']}; "
                f"operators={bundle['operators']}"
            ),
        )
        result["operators"] = bundle["operators"]
        result["capacity_hint"] = bundle["capacity_hint"]
        if "plan" in result:
            plan = result["plan"]
            if isinstance(plan, dict):
                plan["capacity_hint"] = bundle["capacity_hint"]
                plan["operators"] = bundle["operators"]
        return ok(result)
    except PermissionError as exc:
        return err("rate_limited", str(exc), status.HTTP_429_TOO_MANY_REQUESTS)
    except ValueError as exc:
        return err("validation_error", str(exc), status.HTTP_422_UNPROCESSABLE_ENTITY)