import {
  Controller, Get, Post, Patch, Delete, Param, Body, Query,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { WorkspaceService } from './workspace.service';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import type { UserPayload } from '../../common/types';
import {
  RecordRecentDto, AddFavoriteDto, CreateTagDto, UpdateTagDto, AttachTagDto,
} from './dto/workspace.dto';

@ApiTags('workspace')
@Controller('workspace')
export class WorkspaceController {
  constructor(private readonly workspaceService: WorkspaceService) {}

  // ═══ Recent ═══

  @Get('recent')
  @ApiOperation({ summary: '获取最近打开列表' })
  async getRecent(@CurrentUser() user: UserPayload) {
    return this.workspaceService.getRecentItems(String(user?.id || 'anonymous'));
  }

  @Post('recent')
  @ApiOperation({ summary: '记录最近打开' })
  async recordRecent(@CurrentUser() user: UserPayload, @Body() dto: RecordRecentDto) {
    return this.workspaceService.recordRecent(String(user?.id || 'anonymous'), dto);
  }

  // ═══ Favorites ═══

  @Get('favorites')
  @ApiOperation({ summary: '获取收藏列表' })
  async getFavorites(@CurrentUser() user: UserPayload) {
    return this.workspaceService.getFavorites(String(user?.id || 'anonymous'));
  }

  @Post('favorites')
  @ApiOperation({ summary: '添加收藏' })
  async addFavorite(@CurrentUser() user: UserPayload, @Body() dto: AddFavoriteDto) {
    return this.workspaceService.addFavorite(String(user?.id || 'anonymous'), dto);
  }

  @Delete('favorites/:id')
  @ApiOperation({ summary: '取消收藏' })
  async removeFavorite(@CurrentUser() user: UserPayload, @Param('id') id: string) {
    return this.workspaceService.removeFavorite(String(user?.id || 'anonymous'), id);
  }

  // ═══ Tags ═══

  @Get('tags')
  @ApiOperation({ summary: '获取标签列表' })
  async getTags(@CurrentUser() user: UserPayload) {
    return this.workspaceService.getTags(String(user?.id || 'anonymous'));
  }

  @Post('tags')
  @ApiOperation({ summary: '创建标签' })
  async createTag(@CurrentUser() user: UserPayload, @Body() dto: CreateTagDto) {
    return this.workspaceService.createTag(String(user?.id || 'anonymous'), dto);
  }

  @Patch('tags/:id')
  @ApiOperation({ summary: '更新标签' })
  async updateTag(
    @CurrentUser() user: UserPayload,
    @Param('id') id: string,
    @Body() dto: UpdateTagDto,
  ) {
    return this.workspaceService.updateTag(String(user?.id || 'anonymous'), id, dto);
  }

  @Delete('tags/:id')
  @ApiOperation({ summary: '删除标签' })
  async deleteTag(@CurrentUser() user: UserPayload, @Param('id') id: string) {
    return this.workspaceService.deleteTag(String(user?.id || 'anonymous'), id);
  }

  // ═══ Tag attach/detach ═══

  @Get('items/:itemId/tags')
  @ApiOperation({ summary: '获取知识点标签' })
  async getItemTags(@CurrentUser() user: UserPayload, @Param('itemId') itemId: string) {
    return this.workspaceService.getItemTags(String(user?.id || 'anonymous'), itemId);
  }

  @Post('tags/:id/attach')
  @ApiOperation({ summary: '贴标签' })
  async attachTag(
    @CurrentUser() user: UserPayload,
    @Param('id') tagId: string,
    @Body() dto: AttachTagDto,
  ) {
    return this.workspaceService.attachTag(String(user?.id || 'anonymous'), tagId, dto);
  }

  @Delete('tags/:id/detach')
  @ApiOperation({ summary: '去标签' })
  async detachTag(
    @CurrentUser() user: UserPayload,
    @Param('id') tagId: string,
    @Body() dto: AttachTagDto,
  ) {
    return this.workspaceService.detachTag(String(user?.id || 'anonymous'), tagId, dto);
  }

  // ═══ Search ═══

  @Get('search')
  @ApiOperation({ summary: '全局搜索' })
  async search(
    @CurrentUser() user: UserPayload,
    @Query('q') q: string,
    @Query('limit') limit?: string,
    @Query('offset') offset?: string,
  ) {
    return this.workspaceService.search(
      String(user?.id || 'anonymous'),
      q || '',
      Number(limit) || 20,
      Number(offset) || 0,
    );
  }

  @Get('search-history')
  @ApiOperation({ summary: '搜索历史' })
  async getSearchHistory(@CurrentUser() user: UserPayload) {
    return this.workspaceService.getSearchHistory(String(user?.id || 'anonymous'));
  }

  // ═══ Dashboard ═══

  @Get('dashboard')
  @ApiOperation({ summary: '工作台聚合数据' })
  async getDashboard(@CurrentUser() user: UserPayload) {
    return this.workspaceService.getDashboard(String(user?.id || 'anonymous'));
  }
}
