openapi: 3.1.0
info:
  title: Nutrilearn API — Universitas Yatsi Madani
  version: 1.0.0
  description: |
    Dokumentasi resmi endpoint REST API **Nutrilearn** untuk Program Studi Ilmu Gizi Universitas Yatsi Madani (UYM).

    ### Sumber Data Gizi
    Seluruh perhitungan gizi pada API ini bersumber langsung dari database lokal MariaDB:
    - **TKPI (Kemenkes RI):** 5.294 bahan pangan Indonesia (termasuk BDD%, Natrium, Kalium).
    - **NutriSurvey Indonesia:** 1.105 data standar pangan Indonesia.
    - **DBMP PERSAGI:** 164 bahan makanan penukar & peringatan klinis (tinggi natrium, purin, kolesterol, kalium).
    - **AKG Permenkes No. 28 Tahun 2019:** 26 kelompok umur & jenis kelamin.

    ### Autentikasi & Peran (RBAC)
    Semua endpoint `/api/*` (kecuali `/api/news`) membutuhkan sesi login aktif (`Cookie` sesi Laravel + `X-CSRF-TOKEN` untuk method `POST`/`PATCH`/`DELETE`).
    - **`mahasiswa`**: Menu Composer, Log Makan pribadi, Scan Foto Makanan, Asisten Gizi.
    - **`dosen`**: Semua akses mahasiswa + Rekap Mahasiswa & Audit Memori.
    - **`admin`**: Semua akses dosen + Manajemen Akun Kampus.

servers:
  - url: /
    description: Server Nutrilearn Aktif

tags:
  - name: Pangan & AKG
    description: Pencarian bahan pangan TKPI, NutriSurvey, DBMP PERSAGI, dan profil AKG Permenkes 2019.
  - name: Menu Composer & Log
    description: Kalkulator gizi menu berbasis BDD%, evaluasi % AKG, dan penyimpanan log makan harian.
  - name: Scan Foto Makanan
    description: Identifikasi komponen hidangan dari foto dan konfirmasi gramasi ke Menu Composer.
  - name: Asisten Gizi
    description: Chatbot konsultasi gizi berbasis TKPI, DBMP PERSAGI, dan AKG Permenkes 2019.
  - name: Berita Gizi
    description: Endpoint publik berita gizi, nutrisi, dan kesehatan Indonesia per kategori.
  - name: Dosen & Audit
    description: Rekap asupan mahasiswa dan audit hidangan tersimpan (khusus role `dosen` dan `admin`).
  - name: Admin
    description: Pengelolaan peran dan pra-registrasi email dosen kampus (khusus role `admin`).

paths:
  /api/foods/search:
    get:
      tags: [Pangan & AKG]
      summary: Cari bahan pangan (TKPI, NutriSurvey, DBMP)
      description: Mencari bahan pangan berdasarkan nama dengan peringkat relevansi.
      parameters:
        - name: q
          in: query
          required: true
          description: Kata kunci nama bahan makanan (minimal 2 karakter).
          schema:
            type: string
            example: sate sapi
      responses:
        '200':
          description: Daftar bahan pangan yang cocok dari TKPI, NutriSurvey, dan DBMP.
          content:
            application/json:
              schema:
                type: object
                properties:
                  tkpi:
                    type: array
                    items:
                      $ref: '#/components/schemas/FoodTkpiItem'
                  nutrisurvey:
                    type: array
                    items:
                      type: object
                  dbmp:
                    type: array
                    items:
                      type: object
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/akg:
    get:
      tags: [Pangan & AKG]
      summary: Daftar standar AKG Permenkes 2019
      description: Mengembalikan 26 kelompok standar AKG (energi, protein, lemak, karbohidrat, serat, natrium, kalium).
      responses:
        '200':
          description: Daftar kelompok AKG berhasil dimuat.
          content:
            application/json:
              schema:
                type: object
                properties:
                  akg:
                    type: array
                    items:
                      type: object
                      properties:
                        id: { type: integer, example: 8 }
                        kategori: { type: string, example: Laki-laki }
                        kelompok_umur: { type: string, example: 19 - 29 tahun }
                        energi_kkal: { type: number, example: 2650 }
                        protein_g: { type: number, example: 65 }
                        natrium_mg: { type: number, example: 1500 }
                        kalium_mg: { type: number, example: 4700 }
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/profile/akg:
    post:
      tags: [Pangan & AKG]
      summary: Simpan profil AKG pengguna
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [akg_standard_id]
              properties:
                akg_standard_id:
                  type: integer
                  example: 8
      responses:
        '200':
          description: Profil AKG pengguna berhasil diperbarui.
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/menu/calculate:
    post:
      tags: [Menu Composer & Log]
      summary: Hitung gizi menu & evaluasi % AKG
      description: Menghitung energi, makro, natrium, kalium, rasio Na:K, persentase AKG, serta peringatan klinis DBMP berdasarkan berat gramasi dan BDD%.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [items]
              properties:
                akg_standard_id:
                  type: integer
                  nullable: true
                  example: 8
                items:
                  type: array
                  minItems: 1
                  items:
                    type: object
                    required: [kode_pangan, berat_g]
                    properties:
                      kode_pangan: { type: string, example: FP0463 }
                      berat_g: { type: number, example: 100 }
      responses:
        '200':
          description: Hasil perhitungan gizi lengkap beserta evaluasi AKG dan peringatan klinis.
          content:
            application/json:
              schema:
                type: object
                properties:
                  totals:
                    type: object
                    properties:
                      energi_kkal: { type: number, example: 306 }
                      protein_g: { type: number, example: 22.1 }
                      natrium_mg: { type: number, example: 972 }
                      kalium_mg: { type: number, example: 280 }
                      na_k_ratio: { type: number, example: 3.47 }
                  akg_percent:
                    type: object
                  clinical_warnings:
                    type: array
                    items: { type: string }
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/menu/save:
    post:
      tags: [Menu Composer & Log]
      summary: Simpan menu ke riwayat log makan
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [dish_name, meal_type, items]
              properties:
                dish_name: { type: string, example: Makan Siang Kampus }
                meal_type:
                  type: string
                  enum: [pagi, siang, malam, selingan]
                  example: siang
                log_date: { type: string, format: date, example: '2026-12-03' }
                akg_standard_id: { type: integer, nullable: true, example: 8 }
                items:
                  type: array
                  items:
                    type: object
                    required: [kode_pangan, berat_g]
                    properties:
                      kode_pangan: { type: string, example: FP0463 }
                      berat_g: { type: number, example: 100 }
      responses:
        '201':
          description: Log makan berhasil disimpan.
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/menu/logs:
    get:
      tags: [Menu Composer & Log]
      summary: Ambil riwayat log makan pribadi
      responses:
        '200':
          description: Daftar riwayat log makan milik pengguna yang sedang login.
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/scan:
    post:
      tags: [Scan Foto Makanan]
      summary: Pindai foto makanan
      description: Mengunggah foto hidangan (JPEG/PNG/WebP), mengompres gambar, dan mendeteksi bahan penyusun beserta pencocokan awal ke TKPI.
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [image]
              properties:
                image:
                  type: string
                  format: binary
      responses:
        '200':
          description: Daftar bahan makanan terdeteksi beserta perkiraan gramasi dan URT.
        '429':
          description: Kuota scan harian pengguna telah tercapai.
        '401':
          $ref: '#/components/responses/Unauthorized'

  /api/scan/confirm:
    post:
      tags: [Scan Foto Makanan]
      summary: Konfirmasi hasil scan & kirim ke Menu Composer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [dish_name, items]
              properties:
                dish_name: { type: string, example: Nasi Ayam Bakar }
                image_path: { type: string, nullable: true, example: scans/temp/dish_123.jpg }
                akg_standard_id: { type: integer, nullable: true, example: 8 }
                items:
                  type: array
                  items:
                    type: object
                    required: [kode_pangan, berat_g]
                    properties:
                      kode_pangan: { type: string, example: FP0463 }
                      berat_g: { type: number, example: 100 }
                      urt_label: { type: string, nullable: true, example: 1 potong sedang }
      responses:
        '200':
          description: Perhitungan gizi terkonfirmasi dan masuk antrean pembaruan memori hidangan.
        '422':
          description: Validasi input gagal (termasuk path gambar tidak valid).

  /api/chat:
    post:
      tags: [Asisten Gizi]
      summary: Kirim pertanyaan ke Asisten Gizi
      description: Menjawab pertanyaan seputar ilmu gizi, diet klinis, nilai nutrisi TKPI, penukar DBMP PERSAGI, dan target AKG Permenkes 2019.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [message]
              properties:
                message:
                  type: string
                  example: Apa bahan makanan penukar rendah natrium untuk bihun?
                akg_standard_id:
                  type: integer
                  nullable: true
                  example: 8
      responses:
        '200':
          description: Jawaban Asisten Gizi beserta daftar referensi yang digunakan.
          content:
            application/json:
              schema:
                type: object
                properties:
                  reply: { type: string }
                  citations:
                    type: array
                    items: { type: string }
        '429':
          description: Kuota chat harian pengguna telah tercapai.

  /api/news:
    get:
      tags: [Berita Gizi]
      summary: Ambil berita gizi & kesehatan Indonesia (Publik)
      description: Endpoint publik (cache 4 jam per kategori) yang menarik berita gizi dan kesehatan terbaru di Indonesia.
      parameters:
        - name: category
          in: query
          required: false
          description: Kategori berita gizi.
          schema:
            type: string
            enum: [gizi, stunting, diet, klinis]
            default: gizi
      responses:
        '200':
          description: Daftar 5 berita gizi terbaru sesuai kategori.
          content:
            application/json:
              schema:
                type: object
                properties:
                  category: { type: string, example: gizi }
                  items:
                    type: array
                    items:
                      type: object
                      properties:
                        tag: { type: string, example: Gizi & Nutrisi }
                        source: { type: string, example: Kemenkes }
                        title: { type: string }
                        summary: { type: string }
                        highlight: { type: string, example: 03 Des 2026 }
                        url: { type: string, nullable: true }

  /api/dosen/recap:
    get:
      tags: [Dosen & Audit]
      summary: Rekap asupan mahasiswa (Dosen & Admin)
      description: Mengembalikan ringkasan jumlah log, total energi, protein, dan natrium seluruh mahasiswa.
      responses:
        '200':
          description: Daftar rekap mahasiswa.
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/memory:
    get:
      tags: [Dosen & Audit]
      summary: Daftar hidangan tersimpan & antrean pembelajaran (Dosen & Admin)
      responses:
        '200':
          description: Daftar hidangan terverifikasi dan antrean pending.
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/memory/{id}:
    delete:
      tags: [Dosen & Audit]
      summary: Hapus hidangan dari memori (Dosen & Admin)
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: integer }
      responses:
        '200':
          description: Hidangan berhasil dihapus.
        '403':
          $ref: '#/components/responses/Forbidden'

  /api/admin/users:
    get:
      tags: [Admin]
      summary: Daftar pengguna terdaftar (Admin)
      responses:
        '200':
          description: Daftar seluruh akun pengguna Nutrilearn.
        '403':
          $ref: '#/components/responses/Forbidden'
    post:
      tags: [Admin]
      summary: Pra-registrasi akun dosen/admin kampus (Admin)
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name, email, role]
              properties:
                name: { type: string, example: Dr. Siti Gizi }
                email: { type: string, format: email, example: siti.dosen@uym.ac.id }
                role:
                  type: string
                  enum: [mahasiswa, dosen, admin]
                  example: dosen
      responses:
        '201':
          description: Akun kampus berhasil didaftarkan.
        '422':
          description: Email bukan domain resmi kampus.

  /api/admin/users/{id}/role:
    patch:
      tags: [Admin]
      summary: Ubah peran pengguna (Admin)
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: integer }
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [role]
              properties:
                role:
                  type: string
                  enum: [mahasiswa, dosen, admin]
      responses:
        '200':
          description: Peran pengguna berhasil diperbarui.
        '403':
          $ref: '#/components/responses/Forbidden'

components:
  schemas:
    FoodTkpiItem:
      type: object
      properties:
        kode_pangan: { type: string, example: FP0463 }
        nama_pangan: { type: string, example: Sate daging sapi }
        bdd_persen: { type: number, example: 100 }
        energi_kkal: { type: number, example: 306 }
        protein_total_g: { type: number, example: 22.1 }
        lemak_g: { type: number, example: 18.4 }
        karbohidrat_g: { type: number, example: 12.9 }
        natrium_mg: { type: number, example: 972 }
        kalium_mg: { type: number, example: 280 }

  responses:
    Unauthorized:
      description: Pengguna belum login.
    Forbidden:
      description: Peran pengguna tidak memiliki hak akses untuk endpoint ini.
