-- 315_eid_schedule_views_v_schedule_today.sql — 305_eid_schedule_views.sql 분리본 (single-statement). asyncpg 러너가 prepared statement 로 처리해 multi-statement 거부 → 1 stmt/파일. 내용 불변.

CREATE OR REPLACE VIEW v_schedule_today AS
SELECT e.id, e.user_id, e.title, e.kind, e.status, e.priority,
       e.due_at, e.start_at, e.end_at, e.started_at, e.defer_until, e.project_tag
FROM events e
CROSS JOIN LATERAL (
    SELECT (date_trunc('day', now() AT TIME ZONE 'Asia/Seoul') AT TIME ZONE 'Asia/Seoul') AS lo
) b
WHERE (e.status IN ('inbox','next','scheduled','in_progress')
       OR (e.status = 'deferred' AND e.defer_until IS NOT NULL AND e.defer_until <= now()))
  AND (
        (e.kind = 'task'           AND e.due_at     >= b.lo AND e.due_at     < b.lo + interval '1 day')
     OR (e.kind = 'calendar_event' AND e.start_at   >= b.lo AND e.start_at   < b.lo + interval '1 day')
     OR (e.kind = 'activity_log'   AND e.started_at >= b.lo AND e.started_at < b.lo + interval '1 day')
  );
