# Scénario: Modifier un tag
name: tags_update
version: "1.0"
description: Met à jour un tag existant

input:
  required:
    - id
  optional:
    - name
    - color
  validation:
    id:
      type: int
    name:
      type: string
      min_length: 1
      max_length: 50
    color:
      type: string
      max_length: 7

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

  - id: check_unique
    service: db
    action: query_one
    condition: "{{input.name != null}}"
    params:
      query: "SELECT id FROM tags WHERE user_id = ? AND name = ? AND id != ?"
      args: ["{{auth.user_id}}", "{{input.name}}", "{{input.id}}"]
    on_success: abort
    error_message: "Ce tag existe déjà"
    error_status: 409

  - id: update_tag
    service: db
    action: update
    params:
      table: tags
      where:
        id: "{{input.id}}"
      data:
        name: "{{input.name ?? steps.get_tag.result.name}}"
        color: "{{input.color ?? steps.get_tag.result.color}}"

  - id: get_updated
    service: db
    action: query_one
    params:
      query: "SELECT * FROM tags WHERE id = ?"
      args: ["{{input.id}}"]

output:
  status: 200
  body:
    success: true
    message: "Tag mis à jour"
    data: "{{steps.get_updated.result}}"
