openapi: 3.0.3
info:
  title: TIIX Permissions API
  version: "2025-09-01"
  description: Gestão de slugs, grupos, vínculos e consultas de permissões.
servers:
  - url: /adm/api
paths:
  /Permissions/ping:
    get:
      summary: Health-check
      responses: { "200": { description: OK } }

  /Permissions/slugs:
    get:
      summary: Lista slugs
      parameters:
        - in: query; name: q; schema: { type: string }
        - in: query; name: tipo; schema: { type: string, enum: [acao, tela] }
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  slugs:
                    type: array
                    items:
                      type: object
                      properties:
                        id: { type: integer }
                        slug: { type: string }
                        nome: { type: string }
                        descricao: { type: string }
                        tipo: { type: string }
                        id_tela: { type: integer, nullable: true }

  /Permissions/slugs_save:
    post:
      summary: Cria/edita slug
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                data:
                  type: object
                  required: [slug]
                  properties:
                    id: { type: integer }
                    slug: { type: string }
                    nome: { type: string }
                    descricao: { type: string }
                    tipo: { type: string, enum: [acao, tela] }
                    id_tela: { type: integer, nullable: true }
      responses: { "200": { description: OK } }

  /Permissions/slugs_delete:
    post:
      summary: Exclui slug (e vínculos)
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties: { data: { type: object, properties: { id: { type: integer } }, required: [id] } }
      responses: { "200": { description: OK } }

  /Permissions/groups:
    get:
      summary: Lista grupos do contrato
      parameters:
        - in: query; name: id_contrato; schema: { type: integer }
        - in: query; name: q; schema: { type: string }
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  groups:
                    type: array
                    items:
                      type: object
                      properties:
                        id: { type: integer }
                        nome: { type: string }
                        slug: { type: string }
                        id_contrato: { type: integer }
                        total_slugs: { type: integer }
                        total_usuarios: { type: integer }

  /Permissions/groups_save:
    post:
      summary: Cria/edita grupo (por contrato)
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                data:
                  type: object
                  required: [nome, id_contrato]
                  properties:
                    id: { type: integer }
                    nome: { type: string }
                    id_contrato: { type: integer }
      responses: { "200": { description: OK } }

  /Permissions/groups_delete:
    post:
      summary: Exclui grupo (e vínculos)
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties: { data: { type: object, properties: { id: { type: integer } }, required: [id] } }
      responses: { "200": { description: OK } }

  /Permissions/groupSlugs:
    get:
      summary: Lista slugs do grupo
      parameters:
        - in: query; name: group_id; schema: { type: integer, minimum: 1 }
      responses: { "200": { description: OK } }

  /Permissions/groupSlugs_save:
    post:
      summary: Salva slugs de um grupo
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                data:
                  type: object
                  required: [group_id]
                  properties:
                    group_id: { type: integer }
                    slugs:
                      type: array
                      items: { type: string }
      responses: { "200": { description: OK } }

  /Permissions/users:
    get:
      summary: Busca usuários (opcionalmente por contrato)
      parameters:
        - in: query; name: q; schema: { type: string }
        - in: query; name: id_contrato; schema: { type: integer }
      responses: { "200": { description: OK } }

  /Permissions/userGroups:
    get:
      summary: Lista grupos do usuário (opcionalmente filtrando por contrato)
      parameters:
        - in: query; name: user_id; schema: { type: integer, minimum: 1 }
        - in: query; name: id_contrato; schema: { type: integer }
      responses: { "200": { description: OK } }

  /Permissions/userGroups_save:
    post:
      summary: Define grupos do usuário (flush + insert)
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                data:
                  type: object
                  required: [user_id]
                  properties:
                    user_id: { type: integer }
                    id_contrato: { type: integer }
                    groups:
                      type: array
                      items: { type: integer }
      responses: { "200": { description: OK } }

  /Permissions/groupUsers:
    get:
      summary: Lista membros do grupo
      parameters:
        - in: query; name: group_id; schema: { type: integer, minimum: 1 }
      responses: { "200": { description: OK } }

  /Permissions/groupUsers_save:
    post:
      summary: Define membros do grupo (flush + insert)
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                data:
                  type: object
                  required: [group_id]
                  properties:
                    group_id: { type: integer }
                    users:
                      type: array
                      items: { type: integer }
      responses: { "200": { description: OK } }

  /Permissions/userEffectiveSlugs:
    get:
      summary: Slugs efetivos do usuário
      parameters:
        - in: query; name: user_id; schema: { type: integer, minimum: 1 }
        - in: query; name: id_contrato; schema: { type: integer }
      responses: { "200": { description: OK } }

  /Permissions/templates:
    get:
      summary: Lista nomes de templates disponíveis
      responses: { "200": { description: OK } }

  /Permissions/applyTemplate:
    post:
      summary: Aplica template no contrato (cria grupos/slugs e vincula)
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                data:
                  type: object
                  required: [id_contrato, template]
                  properties:
                    id_contrato: { type: integer }
                    template: { type: string }
      responses: { "200": { description: OK } }
