/**
 * SDK 管理 API
 */
import type { SDK, SDKVersion, SDKDownloadRecord, SDKCategory } from '../types/sdk';

// 模拟数据
const mockSDKs: SDK[] = [
  {
    id: '1',
    name: 'JavaScript SDK',
    icon: 'https://cdn.jsdelivr.net/npm/@programming-icons/cdn/javascript@1.0.0/javascript.png',
    description: '适用于 Web 浏览器和 Node.js 的 JavaScript SDK',
    version: '2.8.0',
    language: 'JavaScript',
    category: 'web',
    downloadCount: 125680,
    stars: 3420,
    docsUrl: '/docs/sdk/javascript',
    npmPackage: '@websopy/sdk-js',
    downloadUrl: 'https://www.npmjs.com/package/@websopy/sdk-js',
    changelog: '- 优化性能 20%\n- 新增 TypeScript 支持\n- 修复若干 bug',
    dependencies: ['axios'],
    supportedVersions: ['ES6+', 'Node.js 14+'],
    lastUpdated: '2026-04-10'
  },
  {
    id: '2',
    name: 'TypeScript SDK',
    icon: 'https://cdn.jsdelivr.net/npm/@programming-icons/cdn/typescript@3.0.3/typescript.png',
    description: '完整的 TypeScript 类型支持，适用于现代 Web 开发',
    version: '2.8.0',
    language: 'TypeScript',
    category: 'web',
    downloadCount: 98650,
    stars: 2890,
    docsUrl: '/docs/sdk/typescript',
    npmPackage: '@websopy/sdk-ts',
    downloadUrl: 'https://www.npmjs.com/package/@websopy/sdk-ts',
    changelog: '- 完整的类型定义\n- 更好的 IDE 支持\n- 新增装饰器支持',
    dependencies: [],
    supportedVersions: ['TypeScript 4.0+'],
    lastUpdated: '2026-04-10'
  },
  {
    id: '3',
    name: '微信小程序 SDK',
    icon: 'https://res.wx.qq.com/a/wx_fed/assets/res/NTI4MWU5.ico',
    description: '专为微信小程序优化的 SDK，支持插件机制',
    version: '2.7.5',
    language: 'JavaScript',
    category: 'mini-program',
    downloadCount: 78540,
    stars: 1560,
    docsUrl: '/docs/sdk/wx-mini-program',
    npmPackage: '@websopy/sdk-wx',
    downloadUrl: 'https://www.npmjs.com/package/@websopy/sdk-wx',
    changelog: '- 适配最新微信版本\n- 新增分享能力\n- 优化包体积',
    dependencies: ['wechat-miniprogram'],
    supportedVersions: ['微信小程序 2.0+'],
    lastUpdated: '2026-04-08'
  },
  {
    id: '4',
    name: 'Flutter SDK',
    icon: 'https://cdn.jsdelivr.net/gh/flutter/website@main/src/assets/images/flutter-icon.svg',
    description: 'Flutter 跨平台 SDK，支持 iOS 和 Android',
    version: '1.5.2',
    language: 'Flutter',
    category: 'mobile',
    downloadCount: 45230,
    stars: 980,
    docsUrl: '/docs/sdk/flutter',
    downloadUrl: 'https://pub.dev/packages/websopy_flutter',
    changelog: '- 支持 Flutter 3.0\n- 新增状态管理\n- 优化首屏加载',
    dependencies: ['http', 'shared_preferences'],
    supportedVersions: ['Flutter 2.0+', 'Dart 2.12+'],
    lastUpdated: '2026-04-05'
  },
  {
    id: '5',
    name: 'React Native SDK',
    icon: 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/512px-React-icon.svg.png',
    description: 'React Native 原生模块，完美的性能体验',
    version: '2.4.0',
    language: 'React Native',
    category: 'mobile',
    downloadCount: 38560,
    stars: 870,
    docsUrl: '/docs/sdk/react-native',
    downloadUrl: 'https://www.npmjs.com/package/@websopy/sdk-rn',
    changelog: '- 新增 Hooks API\n- 支持 TypeScript\n- 修复内存泄漏',
    dependencies: ['react-native'],
    supportedVersions: ['React Native 0.65+', 'React 17+'],
    lastUpdated: '2026-04-03'
  },
  {
    id: '6',
    name: 'Java SDK',
    icon: 'https://cdn.jsdelivr.net/npm/@programming-icons/cdn/java@1.0.0/java-original.svg',
    description: '企业级 Java SDK，支持 Spring Boot 集成',
    version: '3.2.0',
    language: 'Java',
    category: 'server',
    downloadCount: 65420,
    stars: 1230,
    docsUrl: '/docs/sdk/java',
    downloadUrl: '/downloads/sdk-java-3.2.0.jar',
    changelog: '- 支持 Spring Boot 3.0\n- 新增响应式编程\n- 优化连接池',
    dependencies: ['slf4j', 'jackson'],
    supportedVersions: ['Java 8+', 'Spring Boot 2.0+'],
    lastUpdated: '2026-04-01'
  },
  {
    id: '7',
    name: 'Go SDK',
    icon: 'https://cdn.jsdelivr.net/npm/@programming-icons/cdn/go@1.0.0/go-original.svg',
    description: '高性能 Go SDK，支持 goroutine 并发',
    version: '2.5.0',
    language: 'Go',
    category: 'server',
    downloadCount: 32180,
    stars: 760,
    docsUrl: '/docs/sdk/go',
    downloadUrl: 'go get github.com/websopy/sdk-go@latest',
    changelog: '- 支持 Go 1.18 泛型\n- 新增中间件支持\n- 优化性能',
    dependencies: [],
    supportedVersions: ['Go 1.16+'],
    lastUpdated: '2026-03-28'
  },
  {
    id: '8',
    name: 'Python SDK',
    icon: 'https://cdn.jsdelivr.net/npm/@programming-icons/cdn/python@3.10.0/python-original.svg',
    description: 'Pythonic 的 Python SDK，支持异步编程',
    version: '2.6.0',
    language: 'Python',
    category: 'server',
    downloadCount: 52100,
    stars: 980,
    docsUrl: '/docs/sdk/python',
    downloadUrl: 'pip install websopy-sdk',
    changelog: '- 支持 asyncio\n- 新增类型提示\n- 兼容 Python 3.11',
    dependencies: ['requests', 'typing-extensions'],
    supportedVersions: ['Python 3.7+'],
    lastUpdated: '2026-03-25'
  },
  {
    id: '9',
    name: 'Swift SDK',
    icon: 'https://cdn.jsdelivr.net/npm/@programming-icons/cdn/swift@1.0.0/swift-original.svg',
    description: '原生 iOS/macOS Swift SDK，支持 SwiftUI',
    version: '1.8.0',
    language: 'Swift',
    category: 'mobile',
    downloadCount: 24560,
    stars: 560,
    docsUrl: '/docs/sdk/swift',
    downloadUrl: 'https://github.com/websopy/sdk-swift/releases',
    changelog: '- 支持 Swift 5.7\n- 新增 Combine 支持\n- 优化包体积',
    dependencies: [],
    supportedVersions: ['iOS 13+', 'macOS 11+', 'Swift 5.0+'],
    lastUpdated: '2026-03-20'
  },
  {
    id: '10',
    name: '.NET SDK',
    icon: 'https://cdn.jsdelivr.net/npm/@programming-icons/cdn/dot-net@1.0.0/dot-net-original.svg',
    description: '.NET 5/6/7 SDK，支持 Entity Framework Core',
    version: '2.3.0',
    language: '.NET',
    category: 'server',
    downloadCount: 28940,
    stars: 450,
    docsUrl: '/docs/sdk/dotnet',
    downloadUrl: 'https://www.nuget.org/packages/WebsopySDK',
    changelog: '- 支持 .NET 7\n- 新增依赖注入\n- 支持 Blazor',
    dependencies: ['Microsoft.Extensions.DependencyInjection'],
    supportedVersions: ['.NET 5+', '.NET Core 3.1+'],
    lastUpdated: '2026-03-18'
  }
];

/**
 * 获取 SDK 分类列表
 */
export async function listSDKCategories(): Promise<SDKCategory[]> {
  return [
    { id: 'web', name: 'Web 开发', icon: 'globe', count: 2, description: '适用于浏览器和 Node.js 环境' },
    { id: 'mobile', name: '移动开发', icon: 'mobile', count: 3, description: 'iOS、Android、Flutter、React Native' },
    { id: 'server', name: '服务端', icon: 'server', count: 4, description: 'Java、Go、Python、.NET 等后端语言' },
    { id: 'mini-program', name: '小程序', icon: 'wechat', count: 1, description: '微信小程序、支付宝小程序等' }
  ];
}

/**
 * 获取 SDK 列表
 */
export async function pageSDK(params: {
  category?: string;
  language?: string;
  keyword?: string;
  page?: number;
  pageSize?: number;
}): Promise<{ list: SDK[]; total: number }> {
  const { category, language, keyword, page = 1, pageSize = 10 } = params;
  
  let filtered = [...mockSDKs];
  
  if (category) {
    filtered = filtered.filter(sdk => sdk.category === category);
  }
  if (language) {
    filtered = filtered.filter(sdk => sdk.language === language);
  }
  if (keyword) {
    const kw = keyword.toLowerCase();
    filtered = filtered.filter(sdk => 
      sdk.name.toLowerCase().includes(kw) || 
      sdk.description.toLowerCase().includes(kw)
    );
  }
  
  const start = (page - 1) * pageSize;
  const list = filtered.slice(start, start + pageSize);
  
  return { list, total: filtered.length };
}

/**
 * 获取 SDK 详情
 */
export async function getSDKDetail(id: string): Promise<SDK | null> {
  return mockSDKs.find(sdk => sdk.id === id) || null;
}

/**
 * 获取 SDK 版本列表
 */
export async function listSDKVersions(id: string): Promise<SDKVersion[]> {
  const sdk = mockSDKs.find(s => s.id === id);
  if (!sdk) return [];
  
  return [
    { version: sdk.version, releaseDate: sdk.lastUpdated, releaseNotes: sdk.changelog, downloadUrl: sdk.downloadUrl, size: '2.5MB', sha256: 'a1b2c3d4e5f6...' },
    { version: '2.7.0', releaseDate: '2026-03-15', releaseNotes: '- 性能优化\n- Bug 修复', downloadUrl: '', size: '2.3MB', sha256: 'b2c3d4e5f6g7...' },
    { version: '2.6.0', releaseDate: '2026-02-20', releaseNotes: '- 新增功能\n- 文档更新', downloadUrl: '', size: '2.1MB', sha256: 'c3d4e5f6g7h8...' }
  ];
}

/**
 * 获取下载记录
 */
export async function pageDownloadRecord(params: {
  page?: number;
  pageSize?: number;
}): Promise<{ list: SDKDownloadRecord[]; total: number }> {
  return {
    list: [
      { id: '1', sdkId: '1', sdkName: 'JavaScript SDK', version: '2.8.0', language: 'JavaScript', userId: '1', userName: '张三', downloadTime: '2026-04-12 14:30:00', ipAddress: '127.0.0.1' },
      { id: '2', sdkId: '3', sdkName: '微信小程序 SDK', version: '2.7.5', language: 'JavaScript', userId: '1', userName: '张三', downloadTime: '2026-04-11 10:20:00', ipAddress: '127.0.0.1' },
      { id: '3', sdkId: '6', sdkName: 'Java SDK', version: '3.2.0', language: 'Java', userId: '1', userName: '张三', downloadTime: '2026-04-10 16:45:00', ipAddress: '127.0.0.1' }
    ],
    total: 3
  };
}

/**
 * 记录 SDK 下载
 */
export async function recordDownload(id: string, version: string): Promise<void> {
  console.log(`Download recorded: ${id} v${version}`);
}

/**
 * 获取 SDK 统计
 */
export async function getSDKStats(): Promise<{
  totalDownloads: number;
  totalSDKs: number;
  topSDKs: SDK[];
}> {
  return {
    totalDownloads: mockSDKs.reduce((sum, sdk) => sum + sdk.downloadCount, 0),
    totalSDKs: mockSDKs.length,
    topSDKs: [...mockSDKs].sort((a, b) => b.downloadCount - a.downloadCount).slice(0, 5)
  };
}
