import { Controller, Get, Post, Param, Query, Body, UseGuards } from '@nestjs/common';
import { AdminAuthGuard } from '../../common/guards/admin-auth.guard';
import { AdminRolesGuard } from '../../common/guards/admin-roles.guard';
import { AdminLearningService } from './admin-learning.service';
import { EventFilterQuery, SessionFilterQuery, ProgressFilterQuery, RecordFilterQuery, PaginationQuery, BatchReprocessDto, RecalculateDto } from './dto/admin-learning.dto';

@Controller('admin-api/learning')
@UseGuards(AdminAuthGuard, AdminRolesGuard)
export class AdminLearningController {
  constructor(private readonly service: AdminLearningService) {}

  // ── Dashboard ──
  @Get('dashboard')
  async getDashboard(
    @Query('knowledgeBaseId') knowledgeBaseId?: string,
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) { return this.service.getDashboard({ knowledgeBaseId, startDate, endDate }); }

  // ── Knowledge Bases ──
  @Get('knowledge-bases')
  async getKnowledgeBases() { return this.service.getKnowledgeBases(); }

  // ── ReadingEvents ──
  @Get('reading-events')
  async getReadingEvents(@Query() query: EventFilterQuery) { return this.service.getReadingEvents(query); }
  @Get('reading-events/:id')
  async getReadingEvent(@Param('id') id: string) { return this.service.getReadingEvent(id); }
  @Post('reading-events/:id/reprocess')
  async reprocessEvent(@Param('id') id: string) { return this.service.reprocessEvent(id); }
  @Post('reading-events/reprocess-batch')
  async batchReprocess(@Body() dto: BatchReprocessDto) { return this.service.batchReprocess(dto.eventIds); }

  // ── Sessions ──
  @Get('sessions')
  async getSessions(@Query() query: SessionFilterQuery) { return this.service.getSessions(query); }
  @Get('sessions/:id')
  async getSession(@Param('id') id: string) { return this.service.getSession(id); }
  @Post('sessions/batch-delete')
  async batchDeleteSessions(@Body() body: { ids: string[] }) {
    return this.service.deleteSessions(body.ids);
  }

  // ── Progress ──
  @Get('progress')
  async getProgress(@Query() query: ProgressFilterQuery) { return this.service.getProgress(query); }
  @Get('progress/:id')
  async getProgressDetail(@Param('id') id: string) { return this.service.getProgressDetail(id); }

  // ── DailyActivities ──
  @Get('daily-activities')
  async getDailyActivities(@Query() query: PaginationQuery & { userId?: string; startDate?: string; endDate?: string }) { return this.service.getDailyActivities(query); }

  // ── Records ──
  @Get('records')
  async getRecords(@Query() query: RecordFilterQuery) { return this.service.getRecords(query); }
  @Get('records/:id')
  async getRecord(@Param('id') id: string) { return this.service.getRecord(id); }

  // ── AI Analysis ──
  @Get('analysis')
  async getAnalysis(@Query('userId') userId?: string, @Query('page') page?: string, @Query('limit') limit?: string) {
    return this.service.getAnalysis({ userId, page: page ? parseInt(page) : undefined, limit: limit ? parseInt(limit) : undefined });
  }

  // ── AI Usage ──
  @Get('ai-usage')
  async getAiUsage(@Query('userId') userId?: string, @Query('model') model?: string, @Query('page') page?: string, @Query('limit') limit?: string) {
    return this.service.getAiUsage({ userId, model, page: page ? parseInt(page) : undefined, limit: limit ? parseInt(limit) : undefined });
  }

  // ── Timeline ──
  @Get('user-timeline')
  async getUserTimeline(@Query('userId') userId: string, @Query('limit') limit?: string) {
    return this.service.getUserTimeline(userId, limit ? parseInt(limit) : undefined);
  }

  // ── Diagnose ──
  @Get('user-diagnose')
  async diagnoseUser(@Query('userId') userId: string) { return this.service.diagnoseUser(userId); }
  @Get('material-diagnose')
  async diagnoseMaterial(@Query('materialId') materialId: string) { return this.service.diagnoseMaterial(materialId); }

  // ── Anomalies ──
  @Get('anomalies')
  async getAnomalies(@Query() query: PaginationQuery) { return this.service.getAnomalies(query); }

  // ── Temporary Materials ──
  @Get('temporary-materials')
  async getTemporaryMaterials(@Query() query: PaginationQuery) { return this.service.getTemporaryMaterials(query); }

  // ── Operations ──
  @Post('recalculate')
  async recalculateLearningData(@Body() dto: RecalculateDto) { return this.service.recalculateLearningData(dto.userId); }
  @Get('export')
  async export(@Query('startDate') startDate?: string, @Query('endDate') endDate?: string, @Query('type') type?: string) {
    return this.service.export({ startDate, endDate, type });
  }
}
