import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { ChannelsService } from '../channels/channels.service';
import { MoviesService } from '../movies/movies.service';
import { SeriesService } from '../series/series.service';
import { CategoriesService } from '../categories/categories.service';
import { UserStatus } from '../common/enums';

@Injectable()
export class AdminService {
  constructor(
    private readonly usersService: UsersService,
    private readonly channelsService: ChannelsService,
    private readonly moviesService: MoviesService,
    private readonly seriesService: SeriesService,
    private readonly categoriesService: CategoriesService,
  ) {}

  async getStats() {
    const [
      totalUsers,
      activeUsers,
      suspendedUsers,
      totalChannels,
      totalMovies,
      totalSeries,
      totalCategories,
    ] = await Promise.all([
      this.usersService.count(),
      this.usersService.countByStatus(UserStatus.ACTIVE),
      this.usersService.countByStatus(UserStatus.SUSPENDED),
      this.channelsService.count(),
      this.moviesService.count(),
      this.seriesService.count(),
      this.categoriesService.count(),
    ]);

    return {
      users: {
        total: totalUsers,
        active: activeUsers,
        suspended: suspendedUsers,
      },
      content: {
        channels: totalChannels,
        movies: totalMovies,
        series: totalSeries,
        categories: totalCategories,
      },
    };
  }

  async getOnlineUsers() {
    // In a production environment, you'd track online users via WebSockets or Redis.
    // Here we return recently active users (created/updated in the last 15 minutes) as a proxy.
    const recentUsers = await this.usersService.getRecentUsers(20);

    return {
      count: recentUsers.length,
      users: recentUsers.map((u) => ({
        id: u.id,
        username: u.username,
        email: u.email,
        status: u.status,
        lastSeen: u.updatedAt,
      })),
    };
  }
}
