// Import NestJS decorators and utilities
import { Inject, Injectable } from '@nestjs/common';

// Import TypeORM decorators for dependency injection
import { InjectRepository } from '@nestjs/typeorm';

// Import TypeORM core classes
import { DataSource, Repository } from 'typeorm';

// Import cache-related dependencies
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';

// Import entity representing the database table
import { QuoteMasterEntity } from 'src/entities/QuoteMaster.entity';

/**
 * QuoteMasterService - Service layer for quote master dashboard business logic
 *
 * This service provides a single method for retrieving a random quote for dashboard display.
 * It's designed for inspirational or promotional quotes on a dashboard/homepage.
 *
 * Key Characteristics:
 * - Single method with specific filtering logic
 * - Returns random active quote (not marked as "home" quote)
 * - Uses caching but not implemented in this method
 * - Lightweight service focused on a specific use case
 *
 * Business Context:
 * - Likely used for displaying inspirational quotes on dashboard
 * - Quotes could be motivational, promotional, or informational
 * - Part of dashboard UI to enhance user experience
 * - "isHome" flag might distinguish homepage quotes from dashboard quotes
 */
@Injectable()
export class QuoteMasterService {
  /**
   * Constructor with dependency injection
   *
   * @param datasource - TypeORM DataSource (injected but not used in this method)
   * @param cacheManager - Cache manager (injected but not used in this method)
   * @param quoteMasterRepo - Repository for QuoteMasterEntity database operations
   */
  constructor(
    private datasource: DataSource,
    @Inject(CACHE_MANAGER) private cacheManager: Cache,
    @InjectRepository(QuoteMasterEntity)
    private readonly quoteMasterRepo: Repository<QuoteMasterEntity>,
  ) {}

  /**
   * getAllQuoteMasterDashboard - Retrieves a random quote for dashboard display
   *
   * This method selects a single random quote that meets specific criteria:
   * 1. Must be active (active = 1)
   * 2. Must NOT be a "home" quote (isHome != 1)
   * 3. Returns random result (ORDER BY RAND())
   *
   * Filtering Logic:
   * - active = 1: Only show quotes that are enabled/turned on
   * - isHome != 1: Exclude quotes designated for homepage (different context)
   * - RAND(): Random selection for variety (different quote each time)
   *
   * Selection Fields:
   * - Only selects quote_master_id and quote_master_name
   * - Excludes other potentially large fields (description, dates, etc.)
   *
   * Performance Considerations:
   * - RAND() can be expensive on large datasets
   * - No caching implemented (though cacheManager is available)
   * - Returns only one record (LIMIT 1 implied by getOne())
   *
   * Business Logic:
   * - Provides fresh/different quote on each dashboard load
   * - Separates dashboard quotes from homepage quotes
   * - Simple inspirational feature for user engagement
   *
   * @returns A single random QuoteMasterEntity with id and name
   */
  async getAllQuoteMasterDashboard() {
    return await this.quoteMasterRepo
      .createQueryBuilder('quote_master')
      .select([
        'quote_master.quote_master_id', // Quote ID
        'quote_master.quote_master_name', // Quote text or title
      ])
      // Filter: Only active quotes
      .where('quote_master.active = :active', { active: 1 })
      // Filter: Exclude "home" quotes (likely for homepage)
      .andWhere('quote_master.isHome != :isHome', { isHome: 1 })
      // Random ordering for variety
      .orderBy('RAND()')
      // Get single result
      .getOne();
  }
}
