import { Request, Response } from 'express';
import type { ArticleItem } from '../src/pages/Article/data';

const articles: ArticleItem[] = [
    {
        id: '1',
        title: '深入浅出 UmiJS 4 核心实战',
        cover: 'https://images.unsplash.com/photo-1633356122544-f134324a6cee?w=800&auto=format&fit=crop&q=60',
        summary: '本文将带你深入了解 UmiJS 4 的核心特性，包括路由加载、插件机制以及与 ProComponents 的深度集成。',
        htmlContent: '<p>UmiJS 是可扩展的企业级前端框架，集成了 React 核心能力...</p>',
        category: '技术框架',
        tags: ['UmiJS', 'React', 'Frontend'],
        status: 'published',
        publishTime: '2024-02-14 10:00:00',
        createdAt: '2024-02-14 09:00:00',
        updatedAt: '2024-02-14 10:00:00',
    },
    {
        id: '2',
        title: 'Ant Design 5.0 设计趋势解析',
        cover: 'https://images.unsplash.com/photo-1586717791821-3f44a563eb4c?w=800&auto=format&fit=crop&q=60',
        summary: 'Ant Design 5.0 带来了全新的设计语言：快乐工作，本文解析了其背后的设计哲学。',
        htmlContent: '<p>快乐工作是 Ant Design 5.0 的核心理念...</p>',
        category: '设计规范',
        tags: ['Ant Design', 'Design', 'UI'],
        status: 'draft',
        createdAt: '2024-02-14 11:00:00',
        updatedAt: '2024-02-14 11:00:00',
    },
];

export default {
    'GET /api/articles': (req: Request, res: Response) => {
        const { title } = req.query;
        let filteredData = [...articles];
        if (title) {
            filteredData = filteredData.filter(item => item.title.includes(title as string));
        }
        res.json({
            data: filteredData,
            total: filteredData.length,
            success: true,
        });
    },
    'GET /api/articles/:id': (req: Request, res: Response) => {
        const article = articles.find((i) => i.id === req.params.id);
        res.json({
            data: article,
            success: !!article,
        });
    },
    'POST /api/articles': (req: Request, res: Response) => {
        const newArticle = {
            ...req.body,
            id: Math.random().toString(36).substr(2, 9),
            createdAt: new Date().toISOString(),
            updatedAt: new Date().toISOString(),
        };
        articles.unshift(newArticle);
        res.json({
            data: newArticle,
            success: true,
        });
    },
    'PUT /api/articles/:id': (req: Request, res: Response) => {
        const index = articles.findIndex((item) => item.id === req.params.id);
        if (index !== -1) {
            articles[index] = { ...articles[index], ...req.body, updatedAt: new Date().toISOString() };
            res.json({ data: articles[index], success: true });
        } else {
            res.status(404).json({ success: false, message: 'Article not found' });
        }
    },
    'DELETE /api/articles/:id': (req: Request, res: Response) => {
        const index = articles.findIndex((item) => item.id === req.params.id);
        if (index !== -1) {
            articles.splice(index, 1);
            res.json({ success: true });
        } else {
            res.status(404).json({ success: false, message: 'Article not found' });
        }
    },
};
