# Scénario: Créer une tâche
name: tasks_create
version: "1.0"
description: Crée une nouvelle tâche

input:
  required:
    - project_id
    - status_id
    - title
  optional:
    - description
    - priority
    - date_start
    - date_end
    - time_estimated
    - time_spent
    - billing
    - position
    - tags
  defaults:
    priority: 5
    time_estimated: 0
    time_spent: 0
    billing: 0
    position: 0
  validation:
    project_id:
      type: int
    status_id:
      type: int
    title:
      type: string
      min_length: 1
      max_length: 255
    description:
      type: string
      max_length: 65535
    priority:
      type: int
    date_start:
      type: string
      format: date
    date_end:
      type: string
      format: date

steps:
  - id: check_project
    service: db
    action: query_one
    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
    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: insert_task
    service: db
    action: insert
    params:
      table: tasks
      data:
        user_id: "{{auth.user_id}}"
        project_id: "{{input.project_id}}"
        status_id: "{{input.status_id}}"
        title: "{{input.title}}"
        description: "{{input.description}}"
        priority: "{{input.priority}}"
        date_start: "{{input.date_start}}"
        date_end: "{{input.date_end}}"
        time_estimated: "{{input.time_estimated}}"
        time_spent: "{{input.time_spent}}"
        billing: "{{input.billing}}"
        position: "{{input.position}}"

  - 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: ["{{steps.insert_task.insert_id}}", "{{tag_id}}", "{{auth.user_id}}"]

  - id: get_task
    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: ["{{steps.insert_task.insert_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: ["{{steps.insert_task.insert_id}}"]

output:
  status: 201
  body:
    success: true
    message: "Tâche créée"
    data:
      id: "{{steps.get_task.result.id}}"
      project_id: "{{steps.get_task.result.project_id}}"
      project_name: "{{steps.get_task.result.project_name}}"
      status_id: "{{steps.get_task.result.status_id}}"
      status_name: "{{steps.get_task.result.status_name}}"
      title: "{{steps.get_task.result.title}}"
      tags: "{{steps.get_tags.result}}"
