import { Controller, Get, Query, Req, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { RolesGuard } from '../../common/guards/roles.guard';
import { Roles } from '../../common/decorators/roles.decorator';
import { UserRole } from '@prisma/client';
import { DashboardService } from './dashboard.service';
import { Request } from 'express';

@ApiTags('dashboard')
@Controller('dashboard')
export class DashboardController {
  constructor(private readonly dashboard: DashboardService) {}

  @Get('summary')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.SUPERADMIN)
  @ApiBearerAuth('bearer')
  @ApiOkResponse({
    description: 'High-level analytics for admin dashboard (SUPERADMIN only)',
  })
  summary() {
    return this.dashboard.summary();
  }

  @Get('shop-analytics')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.SUPERADMIN, UserRole.ADMIN, UserRole.SUBADMIN)
  @ApiBearerAuth('bearer')
  @ApiOkResponse({
    description:
      'Shop dashboard analytics (ADMIN/SUBADMIN: current shop, SUPERADMIN: shopId required)',
  })
  shopAnalytics(
    @Req() req: Request & { user?: { id: string; role: UserRole; shopId?: string | null } },
    @Query('from') from?: string,
    @Query('to') to?: string,
    @Query('shopId') shopId?: string,
  ) {
    return this.dashboard.shopAnalytics(req.user!, {
      from: from?.toString(),
      to: to?.toString(),
      shopId: shopId?.toString(),
    });
  }
}
