"""Task work log routes — /api/tasks/<task_id>/logs."""
import logging

from quart import Blueprint, jsonify, request

from scribe.auth import get_current_user_id, login_required
from scribe.services.task_logs import create_log, delete_log, list_logs, update_log

logger = logging.getLogger(__name__)

task_logs_bp = Blueprint("task_logs", __name__, url_prefix="/api/tasks")


@task_logs_bp.route("/<int:task_id>/logs", methods=["GET"])
@login_required
async def list_logs_route(task_id: int):
    uid = get_current_user_id()
    logs = await list_logs(uid, task_id)
    return jsonify({"logs": [log.to_dict() for log in logs]})


@task_logs_bp.route("/<int:task_id>/logs", methods=["POST"])
@login_required
async def create_log_route(task_id: int):
    uid = get_current_user_id()
    data = await request.get_json()
    content = (data or {}).get("content", "").strip()
    if not content:
        return jsonify({"error": "content is required"}), 400
    duration_minutes = (data or {}).get("duration_minutes")
    if duration_minutes is not None:
        try:
            duration_minutes = int(duration_minutes)
        except (TypeError, ValueError):
            return jsonify({"error": "duration_minutes must be an integer"}), 400
    try:
        log = await create_log(uid, task_id, content, duration_minutes)
    except ValueError as e:
        return jsonify({"error": str(e)}), 404
    return jsonify(log.to_dict()), 201


@task_logs_bp.route("/<int:task_id>/logs/<int:log_id>", methods=["PATCH"])
@login_required
async def update_log_route(task_id: int, log_id: int):
    uid = get_current_user_id()
    data = await request.get_json() or {}
    content = data.get("content")
    if content is not None:
        content = content.strip() or None

    from scribe.services.task_logs import _UNSET
    duration_minutes = data.get("duration_minutes", _UNSET)

    log = await update_log(uid, log_id, content=content, duration_minutes=duration_minutes)
    if log is None:
        return jsonify({"error": "Log entry not found"}), 404
    return jsonify(log.to_dict())


@task_logs_bp.route("/<int:task_id>/logs/<int:log_id>", methods=["DELETE"])
@login_required
async def delete_log_route(task_id: int, log_id: int):
    uid = get_current_user_id()
    deleted = await delete_log(uid, log_id)
    if not deleted:
        return jsonify({"error": "Log entry not found"}), 404
    return "", 204
