# Scénario: Connexion utilisateur
name: login
version: "1.0"
description: Authentifie un utilisateur et retourne un JWT

input:
  required:
    - email
    - password
  validation:
    email:
      type: string
      format: email
    password:
      type: string
      min_length: 1

steps:
  - id: get_user
    service: db
    action: query_one
    params:
      query: "SELECT id, email, name, password FROM users WHERE email = ?"
      args: ["{{input.email}}"]
    on_error: abort
    error_message: "Email ou mot de passe incorrect"
    error_status: 401

  - id: verify_password
    service: auth
    action: verify_password
    params:
      hash: "{{steps.get_user.result.password}}"
      password: "{{input.password}}"
    on_error: abort
    error_message: "Email ou mot de passe incorrect"
    error_status: 401

  - id: generate_token
    service: auth
    action: generate_jwt
    params:
      claims:
        sub: "{{steps.get_user.result.id}}"
        email: "{{steps.get_user.result.email}}"
        name: "{{steps.get_user.result.name}}"

output:
  status: 200
  body:
    success: true
    message: "Connexion réussie"
    data:
      token: "{{steps.generate_token.result.token}}"
      user:
        id: "{{steps.get_user.result.id}}"
        email: "{{steps.get_user.result.email}}"
        name: "{{steps.get_user.result.name}}"
