const app = getApp()
const { request } = require('../../utils/request.js')

Page({
  data: {
    notifications: [],
    loading: false
  },

  onShow() {
    this.fetchNotifications()
    this.readAllNotifications()
  },

  fetchNotifications() {
    this.setData({ loading: true })
    request({ url: '/notifications/' })
      .then(res => {
        // Handle paginated response if any, or direct list
        const list = Array.isArray(res) ? res : (res.results || [])
        this.setData({
          notifications: list,
          loading: false
        })
      })
      .catch(err => {
        console.error(err)
        this.setData({ loading: false })
      })
  },

  readAllNotifications() {
    request({ 
      url: '/notifications/read_all/',
      method: 'POST'
    })
      .then(res => {
        console.log('All read')
      })
      .catch(err => console.error(err))
  }
})
