import { Injectable } from '@nestjs/common';
import { AuditSchedulesService } from '../../audit-schedules/services/audit-schedules.service';
import { toMasterRecord, MasterRecord, MasterCounts } from '../templates/my-schedule-master.types';

/**
 * Data service for the My Schedule Master feature.
 * Reuses AuditSchedulesService.myScheduleMaster() for the scoped query,
 * then shapes the response. No DB access of its own.
 */
@Injectable()
export class MyScheduleMasterService {
  constructor(private readonly auditSchedules: AuditSchedulesService) {}

  /** List + counts for the API (rows kept as-is for the frontend table). */
  async list(userId: number, q: any) {
    return this.auditSchedules.myScheduleMaster(userId, q);
  }

  /**
   * Fetch ALL matching rows (no pagination) flattened into display records,
   * plus counts. Used by the Excel + PDF exporters.
   */
  async allRecords(
    userId: number,
    q: any,
  ): Promise<{ records: MasterRecord[]; counts: MasterCounts }> {
    const { data, counts } = await this.auditSchedules.myScheduleMaster(userId, {
      ...q,
      page: 1,
      limit: 100000,
    });
    const records = (data as any[]).map((row, i) => toMasterRecord(row, i));
    return { records, counts };
  }
}
