# Scénario: Modifier une tâche
name: tasks_update
version: "1.0"
description: Met à jour une tâche existante

input:
  required:
    - id
  optional:
    - project_id
    - status_id
    - title
    - description
    - priority
    - date_start
    - date_end
    - time_estimated
    - time_spent
    - billing
    - position
    - tags
  validation:
    id:
      type: int
    project_id:
      type: int
    status_id:
      type: int
    title:
      type: string
      min_length: 1
      max_length: 255

steps:
  - id: get_task
    service: db
    action: query_one
    params:
      query: "SELECT * FROM tasks WHERE id = ? AND user_id = ?"
      args: ["{{input.id}}", "{{auth.user_id}}"]
    on_error: abort
    error_message: "Tâche non trouvée"
    error_status: 404

  - id: check_project
    service: db
    action: query_one
    condition: "{{input.project_id != null}}"
    params:
      query: "SELECT id FROM projects WHERE id = ? AND user_id = ?"
      args: ["{{input.project_id}}", "{{auth.user_id}}"]
    on_error: abort
    error_message: "Projet invalide"
    error_status: 422

  - id: check_status
    service: db
    action: query_one
    condition: "{{input.status_id != null}}"
    params:
      query: "SELECT id FROM statuses WHERE id = ? AND user_id = ?"
      args: ["{{input.status_id}}", "{{auth.user_id}}"]
    on_error: abort
    error_message: "Statut invalide"
    error_status: 422

  - id: update_task
    service: db
    action: update
    params:
      table: tasks
      where:
        id: "{{input.id}}"
      data:
        project_id: "{{input.project_id ?? steps.get_task.result.project_id}}"
        status_id: "{{input.status_id ?? steps.get_task.result.status_id}}"
        title: "{{input.title ?? steps.get_task.result.title}}"
        description: "{{input.description ?? steps.get_task.result.description}}"
        priority: "{{input.priority ?? steps.get_task.result.priority}}"
        date_start: "{{input.date_start ?? steps.get_task.result.date_start}}"
        date_end: "{{input.date_end ?? steps.get_task.result.date_end}}"
        time_estimated: "{{input.time_estimated ?? steps.get_task.result.time_estimated}}"
        time_spent: "{{input.time_spent ?? steps.get_task.result.time_spent}}"
        billing: "{{input.billing ?? steps.get_task.result.billing}}"
        position: "{{input.position ?? steps.get_task.result.position}}"

  - id: clear_tags
    service: db
    action: delete
    condition: "{{input.tags != null}}"
    params:
      table: task_tags
      where:
        task_id: "{{input.id}}"

  - id: sync_tags
    service: db
    action: exec
    condition: "{{input.tags != null && input.tags | length > 0}}"
    foreach: "{{input.tags}}"
    foreach_as: tag_id
    params:
      query: |
        INSERT INTO task_tags (task_id, tag_id)
        SELECT ?, id FROM tags WHERE id = ? AND user_id = ?
      args: ["{{input.id}}", "{{tag_id}}", "{{auth.user_id}}"]

  - id: get_updated
    service: db
    action: query_one
    params:
      query: |
        SELECT t.*, p.name as project_name, s.name as status_name, s.color as status_color
        FROM tasks t
        LEFT JOIN projects p ON t.project_id = p.id
        LEFT JOIN statuses s ON t.status_id = s.id
        WHERE t.id = ?
      args: ["{{input.id}}"]

  - id: get_tags
    service: db
    action: query
    params:
      query: |
        SELECT t.id, t.name, t.color
        FROM tags t
        JOIN task_tags tt ON t.id = tt.tag_id
        WHERE tt.task_id = ?
      args: ["{{input.id}}"]

output:
  status: 200
  body:
    success: true
    message: "Tâche mise à jour"
    data:
      id: "{{steps.get_updated.result.id}}"
      title: "{{steps.get_updated.result.title}}"
      status_name: "{{steps.get_updated.result.status_name}}"
      tags: "{{steps.get_tags.result}}"
