<template>
  <div>
    <h1>POC Persist</h1>

    <v-btn v-if="!pending" @click="onUpdateClick">Update access</v-btn>

    <v-btn @click="onCreateClick">Create Notification</v-btn>
  </div>
</template>

<script setup lang="ts">
import { useEntityManager } from '~/composables/data/useEntityManager'
import { useAccessProfileStore } from '~/stores/accessProfile'
import { useEntityFetch } from '~/composables/data/useEntityFetch'
import Access from '~/models/Access/Access'
import Notification from '~/models/Core/Notification'

definePageMeta({
  layout: false,
})

const { em } = useEntityManager()

const accessProfile = useAccessProfileStore()

const accessId = accessProfile.currentAccessId

const { fetch } = useEntityFetch()

const { data: access, pending } = await fetch(Access, accessId)

const onUpdateClick = () => {
  if (access.value === null) {
    throw new Error('access is null')
  }

  access.value.updateDate = new Date().toISOString()
  console.log(access.value.id, access.value.updateDate)

  em.persist(access.value)
}


const onCreateClick = async () => {
  const notif = em.newInstance(Notification, { name: 'foo', message: ['bar'] })

  // const notif = new Notification({ name: 'foo', message: ['bar'] })

  const createdNotif = await em.persist(notif)

  console.log(createdNotif)
}
</script>
