-- name: CreateScheduleLog :one
INSERT INTO schedule_logs (schedule_id, bot_id, session_id, started_at)
VALUES ($1, $2, sqlc.narg(session_id)::uuid, now())
RETURNING id, schedule_id, bot_id, session_id, status, result_text, error_message, usage, started_at, completed_at;

-- name: CompleteScheduleLog :one
UPDATE schedule_logs
SET status = $2,
    result_text = $3,
    error_message = $4,
    usage = $5,
    model_id = $6,
    completed_at = now()
WHERE id = $1
RETURNING id, schedule_id, bot_id, session_id, status, result_text, error_message, usage, model_id, started_at, completed_at;

-- name: ListScheduleLogsByBot :many
SELECT id, schedule_id, bot_id, session_id, status, result_text, error_message, usage, started_at, completed_at
FROM schedule_logs
WHERE bot_id = $1
  AND ($2::timestamptz IS NULL OR started_at < $2::timestamptz)
ORDER BY started_at DESC
LIMIT $3;

-- name: ListScheduleLogsBySchedule :many
SELECT id, schedule_id, bot_id, session_id, status, result_text, error_message, usage, started_at, completed_at
FROM schedule_logs
WHERE schedule_id = $1
  AND ($2::timestamptz IS NULL OR started_at < $2::timestamptz)
ORDER BY started_at DESC
LIMIT $3;

-- name: DeleteScheduleLogsByBot :exec
DELETE FROM schedule_logs WHERE bot_id = $1;

-- name: DeleteScheduleLogsBySchedule :exec
DELETE FROM schedule_logs WHERE schedule_id = $1;
