<script setup lang="ts">
import { useAuthSession } from '../../composables/useAuthSession'

definePageMeta({
  title: '我的',
})

const { user } = useAuthSession()
const { fetchData } = useClientApi()

type ProfilePayload = {
  profile: { publicSlug: string | null }
}

const publicSlug = ref<string | null>(null)

onMounted(async () => {
  try {
    const { profile } = await fetchData<ProfilePayload>('/api/me/profile')
    publicSlug.value = profile.publicSlug
  } catch {
    publicSlug.value = null
  }
})
</script>

<template>
  <UContainer class="py-8 space-y-6">
    <div>
      <h1 class="text-2xl font-semibold">
        控制台
      </h1>
      <p class="text-sm text-muted mt-1">
        你好，{{ user?.username }}（{{ user?.role }}）
      </p>
      <UButton
        v-if="publicSlug"
        :to="`/@${publicSlug}`"
        class="mt-3"
        variant="outline"
        target="_blank"
      >
        查看我的公开主页
      </UButton>
    </div>

    <div class="grid gap-3 sm:grid-cols-2">
      <UCard>
        <div class="font-medium">
          个人资料
        </div>
        <p class="text-sm text-muted mt-1">
          公开链接、简介、社交链接
        </p>
        <UButton to="/me/profile" class="mt-3" size="sm">
          编辑
        </UButton>
      </UCard>
      <UCard>
        <div class="font-medium">
          文章
        </div>
        <p class="text-sm text-muted mt-1">
          Markdown 写作与可见性
        </p>
        <UButton to="/me/posts" class="mt-3" size="sm">
          管理
        </UButton>
      </UCard>
      <UCard>
        <div class="font-medium">
          时光机
        </div>
        <UButton to="/me/timeline" class="mt-3" size="sm">
          管理
        </UButton>
      </UCard>
      <UCard>
        <div class="font-medium">
          RSS
        </div>
        <UButton to="/me/rss" class="mt-3" size="sm">
          收件箱
        </UButton>
      </UCard>
      <UCard v-if="user?.role === 'admin'">
        <div class="font-medium">
          应用配置
        </div>
        <p class="text-sm text-muted mt-1">
          站点名称、注册开关
        </p>
        <UButton to="/me/admin/config" class="mt-3" size="sm">
          打开
        </UButton>
      </UCard>
      <UCard v-if="user?.role === 'admin'">
        <div class="font-medium">
          用户管理
        </div>
        <UButton to="/me/admin/users" class="mt-3" size="sm">
          打开
        </UButton>
      </UCard>
      <UCard>
        <div class="font-medium">
          媒体
        </div>
        <p class="text-sm text-muted mt-1">
          资源库上传与复制链接；孤儿图片审查与清理。
        </p>
        <UButton to="/me/media" class="mt-3" size="sm">
          进入
        </UButton>
      </UCard>
      <UCard v-if="user?.role === 'admin'">
        <div class="font-medium">
          文章媒体存储校验
        </div>
        <p class="text-sm text-muted mt-1">
          磁盘与 media_assets 一致性
        </p>
        <UButton to="/me/admin/media-storage" class="mt-3" size="sm">
          打开
        </UButton>
      </UCard>
    </div>
  </UContainer>
</template>
