<script setup lang="ts">
import { computed, ref } from 'vue'
import { onShow, onPullDownRefresh } from '@dcloudio/uni-app'
import AppNavbar from '@/components/layout/AppNavbar.vue'
import AppFloatingTabbar from '@/components/layout/AppFloatingTabbar.vue'
import AppPageShell from '@/components/layout/AppPageShell.vue'
import { usePublicSettings } from '@/composables/usePublicSettings'
import { useTheme } from '@/composables/useTheme'
import { getPosts, getWorks, type Post, type Work } from '@/api'
import { resolveMediaUrl } from '@/utils/request'

const { load, getStringSetting } = usePublicSettings()
const { apply } = useTheme()

const loading = ref(true)
const featuredPost = ref<Post | null>(null)
const latestWork = ref<Work | null>(null)

const siteTitle = computed(() => getStringSetting('site_title', '年糕崽崽'))
// const DEFAULT_FEATURED_BG = '/static/bg/img.png'
const DEFAULT_FEATURED_BG = 'https://blog.nailaoyun.cn/bg/img.png'
const featuredBg = computed(() => {
  const cover = featuredPost.value?.cover
  if (cover) return resolveMediaUrl(String(cover))
  try {
    const raw = getStringSetting('homepage_config')
    if (raw) {
      const cfg = JSON.parse(raw) as { bento?: { featuredPost?: { backgroundImage?: string } } }
      const bg = cfg?.bento?.featuredPost?.backgroundImage?.trim()
      if (bg) return resolveMediaUrl(bg)
    }
  }
  catch { /* ignore */ }
  return DEFAULT_FEATURED_BG
})

async function loadHome() {
  apply()
  loading.value = true
  await load()
  try {
    const postsRes = await getPosts({ page: 1, pageSize: 1 })
    const list = Array.isArray(postsRes) ? postsRes : (postsRes as { list?: Post[] }).list || []
    featuredPost.value = list[0] || null
  }
  catch { featuredPost.value = null }
  try {
    const works = await getWorks()
    latestWork.value = works?.[0] || null
  }
  catch { latestWork.value = null }
  loading.value = false
}

onShow(loadHome)

/** 下拉刷新：重新拉首页数据 */
onPullDownRefresh(async () => {
  try {
    await loadHome()
  }
  finally {
    uni.stopPullDownRefresh()
  }
})

const goPost = () => {
  if (featuredPost.value?.id) {
    uni.navigateTo({ url: `/subPackages/content/pages/blog/detail?id=${featuredPost.value.id}` })
  }
}
const goWork = () => {
  if (latestWork.value?.id) {
    uni.navigateTo({ url: `/subPackages/content/pages/works/detail?id=${latestWork.value.id}` })
  }
}
const goExplore = () => {
  apply()
  uni.redirectTo({ url: '/pages/explore/index' })
}
const goSnippets = () => uni.navigateTo({ url: '/subPackages/content/pages/snippets/index' })
</script>

<template>
  <AppPageShell with-tab-pad>
    <AppNavbar :title="siteTitle" />
    <view class="page-pad">
      <view class="hero animate-reveal">
        <view class="hero-eyebrow">Digital Garden</view>
        <view class="hero-title font-serif-italic">
          <text class="line opacity">记录思考</text>
          <text class="line bold">与创造</text>
        </view>
        <view class="hero-bio text-art-muted">
          欢迎来到 <text class="text-art-text">{{ siteTitle }}</text> 的数字花园。
        </view>
        <view class="cta" @click="goExplore">
          <text>探索内容</text>
          <wd-icon name="arrow-right" size="16px" />
        </view>
      </view>

      <view v-if="latestWork" class="work-float art-card art-card-press animate-reveal-delay" @click="goWork">
        <text class="font-mono-label text-art-muted">Latest Work</text>
        <text class="work-title font-serif-italic">{{ latestWork.title }}</text>
      </view>

      <view class="section-head animate-reveal-delay">
        <text class="section-title">此刻</text>
        <text class="font-mono-label text-art-muted">Bento</text>
      </view>

      <view v-if="featuredPost" class="featured art-card art-card-press animate-reveal-delay" @click="goPost">
        <!--        <image class="featured-bg" :src="featuredBg" mode="aspectFill" /> -->
        <image class="featured-bg" src="https://blog.nailaoyun.cn/bg/img.png" mode="aspectFill" />
        <view class="featured-mask" />
        <view class="featured-body">
          <view class="meta-row">
            <text class="tag">{{ featuredPost.categoryName || '未分类' }}</text>
            <text v-if="featuredPost.date" class="date">{{ featuredPost.date }}</text>
          </view>
          <text class="featured-title">{{ featuredPost.title }}</text>
          <view v-if="featuredPost.userName" class="author-row">
            <image
              class="author-avatar"
              :src="resolveMediaUrl(featuredPost.userAvatar) || '/static/default-avatar.svg'"
              mode="aspectFill"
            />
            <text class="author">{{ featuredPost.userName }}</text>
          </view>
        </view>
      </view>

      <view class="grid-2 animate-reveal-delay">
        <view class="art-card art-card-press cell" @click="goExplore">
          <text class="stat">∞</text>
          <text class="text-art-muted">功能入口</text>
        </view>
        <view class="art-card art-card-press cell" @click="goSnippets">
          <text class="code-hint font-mono-label">snippets</text>
          <text class="text-art-text">代码片段 →</text>
        </view>
      </view>

      <wd-loading v-if="loading" />
    </view>
    <AppFloatingTabbar />
  </AppPageShell>
</template>

<style scoped lang="scss">
/* 英雄区整体水平居中，字号/字重/颜色保持原样 */
.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}
.hero-title {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8rpx;
  margin-bottom: 32rpx;
  text-align: center;
  .line { font-size: 72rpx; line-height: 1.15; color: rgb(var(--art-text)); }
  .opacity { opacity: 0.8; }
  .bold { font-style: normal; font-weight: 700; }
}
.hero-bio {
  font-size: 30rpx;
  line-height: 1.7;
  margin-bottom: 40rpx;
  text-align: center;
}
.cta {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 12rpx;
  border-bottom: 1px solid rgb(var(--art-text));
  padding-bottom: 8rpx;
  margin-bottom: 48rpx;
  font-size: 28rpx;
  transition: color 0.25s ease, border-color 0.25s ease;
}
.cta:active { color: rgb(var(--art-accent)); border-color: rgb(var(--art-accent)); }
.work-float {
  padding: 32rpx;
  margin-bottom: 48rpx;
  display: flex;
  flex-direction: column;
  gap: 16rpx;
  min-height: 200rpx;
  justify-content: space-between;
}
.work-title { font-size: 40rpx; color: rgb(var(--art-text)); }
.section-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  border-bottom: 1px solid var(--art-border);
  padding-bottom: 16rpx;
  margin-bottom: 24rpx;
}
.featured {
  position: relative;
  height: 360rpx;
  margin-bottom: 24rpx;
  border-radius: 24rpx;
}
.featured-bg { position: absolute; inset: 0; width: 100%; height: 100%; }
.featured-mask {
  position: absolute; inset: 0;
  background: linear-gradient(to top, rgba(0,0,0,0.82) 0%, rgba(0,0,0,0.35) 45%, rgba(0,0,0,0.1) 100%);
}
.featured-body {
  position: absolute; left: 32rpx; right: 32rpx; bottom: 32rpx; z-index: 2;
  display: flex; flex-direction: column; gap: 12rpx;
}
.meta-row { display: flex; align-items: center; gap: 16rpx; }
.tag {
  font-size: 20rpx;
  padding: 4rpx 12rpx;
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 999rpx;
  color: #fff;
}
.date { font-size: 22rpx; color: rgba(255,255,255,0.65); }
.featured-title { color: #fff; font-size: 36rpx; font-family: 'Playfair Display', serif; text-shadow: 0 2rpx 12rpx rgba(0,0,0,0.35); }
.author-row { display: flex; align-items: center; gap: 12rpx; }
.author-avatar {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.35);
  background: rgba(255,255,255,0.15);
}
.author { font-size: 24rpx; color: rgba(255,255,255,0.75); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 24rpx; }
.cell { padding: 32rpx; min-height: 200rpx; display: flex; flex-direction: column; justify-content: space-between; }
.stat { font-size: 56rpx; font-family: 'JetBrains Mono', monospace; color: rgb(var(--art-text)); }
.code-hint { color: rgb(var(--art-muted)); margin-bottom: 16rpx; }
</style>
