import { Controller, Get, Query } from '@nestjs/common';
import {
  EngagementService,
  EngagementOverview,
  PageMetrics,
  EventMetrics,
  EventByPageMetrics,
  ScrollDepthMetrics,
  UserFlow,
  NavigationFlowData,
  CorpEngagementRow,
} from './engagement.service';
import {
  EngagementQueryDto,
  PageQueryDto,
  EventQueryDto,
  ScrollDepthQueryDto,
  UserFlowQueryDto,
  NavigationFlowsQueryDto,
} from './dto/engagement-query.dto';

@Controller('engagement')
export class EngagementController {
  constructor(private readonly engagementService: EngagementService) {}

  /**
   * Get engagement overview
   * GET /engagement/overview?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('overview')
  async getOverview(@Query() query: EngagementQueryDto): Promise<EngagementOverview> {
    return this.engagementService.getOverview(query);
  }

  /**
   * Get page performance
   * GET /engagement/pages?startDate=2024-01-01&endDate=2024-01-31&sortBy=views
   */
  @Get('pages')
  async getPages(@Query() query: PageQueryDto): Promise<PageMetrics[]> {
    return this.engagementService.getPages(query);
  }

  /**
   * Get event breakdown
   * GET /engagement/events?startDate=2024-01-01&endDate=2024-01-31&category=click
   */
  @Get('events')
  async getEvents(@Query() query: EventQueryDto): Promise<EventMetrics[]> {
    return this.engagementService.getEvents(query);
  }

  /**
   * Get event breakdown grouped by page
   * GET /engagement/events/by-page?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('events/by-page')
  async getEventsByPage(@Query() query: EventQueryDto): Promise<EventByPageMetrics[]> {
    return this.engagementService.getEventsByPage(query);
  }

  /**
   * Get scroll depth metrics
   * GET /engagement/scroll-depth?startDate=2024-01-01&endDate=2024-01-31&page=/profile
   */
  @Get('scroll-depth')
  async getScrollDepth(@Query() query: ScrollDepthQueryDto): Promise<ScrollDepthMetrics[]> {
    return this.engagementService.getScrollDepth(query);
  }

  /**
   * Get user flow analysis
   * GET /engagement/user-flow?startDate=2024-01-01&endDate=2024-01-31&startPage=/
   */
  @Get('user-flow')
  async getUserFlow(@Query() query: UserFlowQueryDto): Promise<UserFlow[]> {
    return this.engagementService.getUserFlow(query);
  }

  /**
   * Get page-to-page navigation flows
   * GET /engagement/navigation/flows?from=/gallery&startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('navigation/flows')
  async getNavigationFlows(@Query() query: NavigationFlowsQueryDto): Promise<NavigationFlowData> {
    return this.engagementService.getNavigationFlows(query);
  }

  /**
   * Per-corp engagement leaderboard (cross-corp; ignores corp filter).
   * GET /engagement/by-corp?startDate=2024-01-01&endDate=2024-01-31
   */
  @Get('by-corp')
  async getByCorp(@Query() query: EngagementQueryDto): Promise<CorpEngagementRow[]> {
    return this.engagementService.getByCorp(query);
  }
}
