# Scénario: Créer un statut
name: statuses_create
version: "1.0"
description: Crée un nouveau statut

input:
  required:
    - code
    - name
  optional:
    - color
    - project_id
    - position
  defaults:
    color: "#6B7280"
  validation:
    code:
      type: int
    name:
      type: string
      min_length: 1
      max_length: 50
    color:
      type: string
      max_length: 7
    project_id:
      type: int
    position:
      type: int

steps:
  - 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: insert_status
    service: db
    action: insert
    params:
      table: statuses
      data:
        user_id: "{{auth.user_id}}"
        project_id: "{{input.project_id}}"
        code: "{{input.code}}"
        name: "{{input.name}}"
        color: "{{input.color}}"
        position: "{{input.position ?? input.code}}"

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

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