# Scénario: Créer un tag
name: tags_create
version: "1.0"
description: Crée un nouveau tag

input:
  required:
    - name
  optional:
    - color
  defaults:
    color: "#3B82F6"
  validation:
    name:
      type: string
      min_length: 1
      max_length: 50
    color:
      type: string
      max_length: 7

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

  - id: insert_tag
    service: db
    action: insert
    params:
      table: tags
      data:
        user_id: "{{auth.user_id}}"
        name: "{{input.name}}"
        color: "{{input.color}}"

  - id: get_tag
    service: db
    action: query_one
    params:
      query: "SELECT * FROM tags WHERE id = ?"
      args: ["{{steps.insert_tag.insert_id}}"]

output:
  status: 201
  body:
    success: true
    message: "Tag créé"
    data: "{{steps.get_tag.result}}"
