import Router from '@koa/router';
import { bgmService } from './bgm.service';

const router = new Router();

/**
 * 获取背景音乐列表
 * GET /api/bgm
 */
router.get('/', async (ctx) => {
  try {
    const { mood } = ctx.query as { mood?: string };

    let bgmList;
    if (mood) {
      bgmList = bgmService.getBgmByMood(mood);
    } else {
      bgmList = bgmService.getBgmList();
    }

    ctx.body = {
      code: 0,
      message: 'success',
      data: bgmList,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '获取背景音乐列表失败',
    };
  }
});

/**
 * 获取所有风格
 * GET /api/bgm/moods
 */
router.get('/moods', async (ctx) => {
  try {
    const moods = bgmService.getMoods();
    ctx.body = {
      code: 0,
      message: 'success',
      data: moods,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '获取风格列表失败',
    };
  }
});

/**
 * 获取单个背景音乐
 * GET /api/bgm/:id
 */
router.get('/:id', async (ctx) => {
  try {
    const { id } = ctx.params;
    const bgm = bgmService.getBgmById(parseInt(id));

    if (!bgm) {
      ctx.body = {
        code: 404,
        message: '背景音乐不存在',
      };
      return;
    }

    ctx.body = {
      code: 0,
      message: 'success',
      data: bgm,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '获取背景音乐失败',
    };
  }
});

/**
 * 添加背景音乐
 * POST /api/bgm
 */
router.post('/', async (ctx) => {
  try {
    const { name, url, mood, duration } = ctx.request.body as {
      name: string;
      url: string;
      mood: string;
      duration: number;
    };

    if (!name || !url || !mood || !duration) {
      ctx.body = {
        code: 400,
        message: '缺少必填字段',
      };
      return;
    }

    const bgm = bgmService.addBgm({ name, url, mood, duration });
    ctx.body = {
      code: 0,
      message: 'success',
      data: bgm,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '添加背景音乐失败',
    };
  }
});

/**
 * 更新背景音乐
 * PUT /api/bgm/:id
 */
router.put('/:id', async (ctx) => {
  try {
    const { id } = ctx.params;
    const { name, url, mood, duration, size } = ctx.request.body as {
      name?: string;
      url?: string;
      mood?: string;
      duration?: number;
      size?: number;
    };

    const bgm = bgmService.updateBgm(parseInt(id), { name, url, mood, duration, size });

    if (!bgm) {
      ctx.body = {
        code: 404,
        message: '背景音乐不存在',
      };
      return;
    }

    ctx.body = {
      code: 0,
      message: 'success',
      data: bgm,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '更新背景音乐失败',
    };
  }
});

/**
 * 删除背景音乐
 * DELETE /api/bgm/:id
 */
router.delete('/:id', async (ctx) => {
  try {
    const { id } = ctx.params;
    const success = bgmService.deleteBgm(parseInt(id));

    if (!success) {
      ctx.body = {
        code: 404,
        message: '背景音乐不存在',
      };
      return;
    }

    ctx.body = {
      code: 0,
      message: 'success',
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '删除背景音乐失败',
    };
  }
});

export default router;