# Scénario: Inscription utilisateur
name: register
version: "1.0"
description: Crée un nouvel utilisateur

input:
  required:
    - email
    - password
    - name
  validation:
    email:
      type: string
      format: email
      max_length: 255
    password:
      type: string
      min_length: 6
      max_length: 255
    name:
      type: string
      min_length: 2
      max_length: 100

steps:
  - id: check_email
    service: db
    action: query_one
    params:
      query: "SELECT id FROM users WHERE email = ?"
      args: ["{{input.email}}"]
    on_success: abort
    error_message: "Cet email est déjà utilisé"
    error_status: 409

  - id: hash_password
    service: auth
    action: hash_password
    params:
      password: "{{input.password}}"

  - id: create_user
    service: db
    action: insert
    params:
      table: users
      data:
        email: "{{input.email}}"
        password: "{{steps.hash_password.result.hash}}"
        name: "{{input.name}}"

  - id: create_default_statuses
    service: db
    action: exec
    params:
      query: |
        INSERT INTO statuses (user_id, project_id, code, name, color, position) VALUES
        (?, NULL, 10, 'Backlog', '#6B7280', 10),
        (?, NULL, 20, 'À faire', '#3B82F6', 20),
        (?, NULL, 30, 'En cours', '#F59E0B', 30),
        (?, NULL, 40, 'À tester', '#8B5CF6', 40),
        (?, NULL, 50, 'Livré', '#10B981', 50),
        (?, NULL, 60, 'Terminé', '#059669', 60),
        (?, NULL, 70, 'Archivé', '#9CA3AF', 70)
      args:
        - "{{steps.create_user.insert_id}}"
        - "{{steps.create_user.insert_id}}"
        - "{{steps.create_user.insert_id}}"
        - "{{steps.create_user.insert_id}}"
        - "{{steps.create_user.insert_id}}"
        - "{{steps.create_user.insert_id}}"
        - "{{steps.create_user.insert_id}}"

  - id: generate_token
    service: auth
    action: generate_jwt
    params:
      claims:
        sub: "{{steps.create_user.insert_id}}"
        email: "{{input.email}}"
        name: "{{input.name}}"

output:
  status: 201
  body:
    success: true
    message: "Inscription réussie"
    data:
      token: "{{steps.generate_token.result.token}}"
      user:
        id: "{{steps.create_user.insert_id}}"
        email: "{{input.email}}"
        name: "{{input.name}}"
