/**
 * 用户作品库 dev 模式 mock 数据
 *
 * 仅用于前端 UI 调试。生产环境通过 import.meta.env.DEV 判断不引用此文件。
 *
 * 提供 5 条覆盖全部状态的 mock 作品 + 单条作品的完整详情（含 13 页内容）。
 * 详见 docs/design/public/ugc-work-status-redesign.md
 */
import type { UserWork, UserWorkPage } from '@/api/public'

/** 3:4 SVG 渐变占位封面（作品库卡片用） */
function mockCover(hue: number): string {
  return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(
    `<svg xmlns="http://www.w3.org/2000/svg" width="300" height="400" viewBox="0 0 300 400">` +
    `<defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1">` +
    `<stop offset="0" stop-color="hsl(${hue},75%,72%)"/>` +
    `<stop offset="1" stop-color="hsl(${(hue + 35) % 360},80%,58%)"/>` +
    `</linearGradient></defs>` +
    `<rect width="300" height="400" fill="url(#g)"/>` +
    `</svg>`
  )
}

/** 16:9 SVG 渐变占位插图（详情页内页用） */
function mockPage(hue: number): string {
  return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(
    `<svg xmlns="http://www.w3.org/2000/svg" width="800" height="450" viewBox="0 0 800 450">` +
    `<defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1">` +
    `<stop offset="0" stop-color="hsl(${hue},70%,75%)"/>` +
    `<stop offset="1" stop-color="hsl(${(hue + 45) % 360},75%,55%)"/>` +
    `</linearGradient></defs>` +
    `<rect width="800" height="450" fill="url(#g)"/>` +
    `</svg>`
  )
}

const baseTime = '2026-04-09T10:00:00Z'
const mockCreator = { id: 1, nickname: '小创作者', avatar: null, username: 'demo' }

/** 5 条 mock 作品列表，每种状态各一条 */
export const MOCK_USER_WORKS: UserWork[] = [
  {
    id: 101,
    userId: 1,
    title: '森林大冒险',
    coverUrl: mockCover(280),
    description: '小主角和小狐狸在魔法森林里寻找会发光的大树',
    visibility: 'private',
    status: 'unpublished',
    reviewNote: null,
    originalImageUrl: mockCover(40),
    voiceInputUrl: null,
    textInput: null,
    aiMeta: null,
    viewCount: 0,
    likeCount: 0,
    favoriteCount: 0,
    commentCount: 0,
    shareCount: 0,
    publishTime: null,
    createTime: baseTime,
    modifyTime: baseTime,
    creator: mockCreator,
    _count: { pages: 13, likes: 0, favorites: 0, comments: 0 },
  },
  {
    id: 102,
    userId: 1,
    title: '正在创作中…',
    coverUrl: null,
    description: null,
    visibility: 'private',
    status: 'draft',
    reviewNote: null,
    originalImageUrl: mockCover(60),
    voiceInputUrl: null,
    textInput: null,
    aiMeta: null,
    viewCount: 0,
    likeCount: 0,
    favoriteCount: 0,
    commentCount: 0,
    shareCount: 0,
    publishTime: null,
    createTime: baseTime,
    modifyTime: baseTime,
    creator: mockCreator,
    _count: { pages: 0, likes: 0, favorites: 0, comments: 0 },
  },
  {
    id: 103,
    userId: 1,
    title: '海底奇遇',
    coverUrl: mockCover(200),
    description: '小章鱼和小海马的友谊故事',
    visibility: 'public',
    status: 'pending_review',
    reviewNote: null,
    originalImageUrl: mockCover(80),
    voiceInputUrl: null,
    textInput: null,
    aiMeta: null,
    viewCount: 0,
    likeCount: 0,
    favoriteCount: 0,
    commentCount: 0,
    shareCount: 0,
    publishTime: null,
    createTime: baseTime,
    modifyTime: baseTime,
    creator: mockCreator,
    _count: { pages: 12, likes: 0, favorites: 0, comments: 0 },
  },
  {
    id: 104,
    userId: 1,
    title: '太空之旅',
    coverUrl: mockCover(260),
    description: '小宇航员驾驶飞船去探索遥远星球',
    visibility: 'public',
    status: 'published',
    reviewNote: null,
    originalImageUrl: mockCover(120),
    voiceInputUrl: null,
    textInput: null,
    aiMeta: null,
    viewCount: 128,
    likeCount: 23,
    favoriteCount: 8,
    commentCount: 5,
    shareCount: 2,
    publishTime: '2026-04-08T15:30:00Z',
    createTime: '2026-04-08T14:00:00Z',
    modifyTime: '2026-04-08T15:30:00Z',
    creator: mockCreator,
    _count: { pages: 14, likes: 23, favorites: 8, comments: 5 },
  },
  {
    id: 105,
    userId: 1,
    title: '魔法学校',
    coverUrl: mockCover(330),
    description: '小巫师入学魔法学校的第一天',
    visibility: 'public',
    status: 'rejected',
    reviewNote: '内容包含疑似版权角色，请修改后重新提交',
    originalImageUrl: mockCover(160),
    voiceInputUrl: null,
    textInput: null,
    aiMeta: null,
    viewCount: 0,
    likeCount: 0,
    favoriteCount: 0,
    commentCount: 0,
    shareCount: 0,
    publishTime: null,
    createTime: baseTime,
    modifyTime: baseTime,
    creator: mockCreator,
    _count: { pages: 11, likes: 0, favorites: 0, comments: 0 },
  },
]

/** 13 页绘本内容（用于详情页阅读器） */
const mockPageTexts = [
  '一个阳光明媚的早晨，小主角推开窗，看见远处的森林闪着金光。',
  '它沿着小路走啊走，遇到了一只迷路的小鸟，羽毛湿漉漉的。',
  '小主角轻轻抱起小鸟，决定送它回家。',
  '路过一片野花田时，一只小狐狸从草丛里跳出来打招呼。',
  '小狐狸说它认识森林里所有的小路，愿意做大家的向导。',
  '三个朋友来到一条清澈的溪水边，溪里有一群闪闪发光的小鱼。',
  '小鱼们告诉他们，那棵会发光的大树就在前方不远处。',
  '森林深处真的有一棵会发光的大树，挂满了亮晶晶的果实。',
  '原来这就是小鸟的家，妈妈正在树枝上焦急地张望。',
  '小鸟欢快地飞回妈妈身边，鸟妈妈给大家每人一颗果实。',
  '夕阳下，小主角和小狐狸告别，小狐狸送他们到森林边缘。',
  '小主角带着这份美好回到家，心里也开出了一朵花。',
]

/** 生成 mock 详情页（含 13 页绘本内容） */
function generateMockPages(workId: number): UserWorkPage[] {
  return mockPageTexts.map((text, i) => ({
    id: workId * 100 + i + 1,
    workId,
    pageNo: i + 1,
    imageUrl: mockPage((280 + i * 27) % 360),
    text,
    audioUrl: null as any,
  })) as UserWorkPage[]
}

/** 根据 id 获取 mock 作品的完整详情（含 pages） */
export function getMockWorkDetail(id: number): UserWork | null {
  const base = MOCK_USER_WORKS.find(w => w.id === id)
  if (!base) return null

  return {
    ...base,
    pages: base.status === 'draft' ? [] : generateMockPages(id),
    tags: [
      { tag: { id: 1, name: '冒险', category: 'theme' } },
      { tag: { id: 2, name: '友谊', category: 'theme' } },
      { tag: { id: 3, name: '成长', category: 'theme' } },
    ],
  }
}

/** 判断一个 id 是否是 mock 作品 id（100-200 之间） */
export function isMockWorkId(id: number): boolean {
  return id >= 100 && id < 200
}
