import Router from '@koa/router';
import { Context } from 'koa';
import * as AudioService from './audio.service';
import { BadRequestError, NotFoundError } from '../../middleware/errorHandler';
import { optionalAuth } from '../../middleware/auth';

const router = new Router();

// 获取音频列表
router.get('/list', optionalAuth, async (ctx: Context) => {
  const userId = ctx.state.user?.userId;
  const { page = 1, pageSize = 10, isFavorite, keyword, category } = ctx.query as {
    page?: string;
    pageSize?: string;
    isFavorite?: string;
    keyword?: string;
    category?: string;
  };

  const result = await AudioService.getAudioList(userId || '', {
    page: Number(page) || 1,
    pageSize: Number(pageSize) || 10,
    isFavorite: isFavorite === 'true' ? true : isFavorite === 'false' ? false : undefined,
    keyword,
    category,
  });

  ctx.body = {
    code: 0,
    message: 'success',
    data: result,
  };
});

// 获取收藏列表
router.get('/favorites', optionalAuth, async (ctx: Context) => {
  const userId = ctx.state.user?.userId;
  const { page = 1, pageSize = 20 } = ctx.query as {
    page?: string;
    pageSize?: string;
  };

  const result = await AudioService.getAudioList(userId || '', {
    page: Number(page) || 1,
    pageSize: Number(pageSize) || 20,
    isFavorite: true,
  });

  ctx.body = {
    code: 0,
    message: 'success',
    data: result,
  };
});

// 获取单个音频详情
router.get('/:id', optionalAuth, async (ctx: Context) => {
  const userId = ctx.state.user?.userId;
  const { id } = ctx.params;

  const audio = await AudioService.getAudioById(id, userId);

  if (!audio) {
    throw new NotFoundError('音频不存在');
  }

  ctx.body = {
    code: 0,
    message: 'success',
    data: audio,
  };
});

// 删除音频
router.delete('/:id', optionalAuth, async (ctx: Context) => {
  const userId = ctx.state.user?.userId;
  const { id } = ctx.params;

  const success = await AudioService.deleteAudio(id, userId);

  if (!success) {
    throw new NotFoundError('音频不存在或无权删除');
  }

  ctx.body = {
    code: 0,
    message: '删除成功',
    data: null,
  };
});

// 切换收藏状态
router.put('/:id/favorite', optionalAuth, async (ctx: Context) => {
  const userId = ctx.state.user?.userId;
  const { id } = ctx.params;

  const audio = await AudioService.toggleFavorite(id, userId);

  if (!audio) {
    throw new NotFoundError('音频不存在');
  }

  ctx.body = {
    code: 0,
    message: 'success',
    data: { isFavorite: audio.isFavorite },
  };
});

// 更新音频信息
router.put('/:id', optionalAuth, async (ctx: Context) => {
  const userId = ctx.state.user?.userId;
  const { id } = ctx.params;
  const { title, tags } = ctx.request.body as { title?: string; tags?: string[] };

  const audio = await AudioService.updateAudio(id, userId, { title, tags });

  if (!audio) {
    throw new NotFoundError('音频不存在');
  }

  ctx.body = {
    code: 0,
    message: '更新成功',
    data: audio,
  };
});

// 获取用户统计
router.get('/stats/overview', optionalAuth, async (ctx: Context) => {
  const userId = ctx.state.user?.userId;
  const stats = await AudioService.getUserStats(userId);

  ctx.body = {
    code: 0,
    message: 'success',
    data: stats,
  };
});

export default router;