import type { IServiceContainer } from '@stock-bot/handlers';
import { getLogger } from '@stock-bot/logger';
import type { JobPayload, SyncStatus } from '../../../types/job-payloads';

const logger = getLogger('enhanced-sync-status');

export async function getSyncStatus(
  payload: JobPayload,
  container: IServiceContainer
): Promise<SyncStatus[]> {
  logger.info('Getting comprehensive sync status...');

  try {
    const postgresClient = container.postgres;
    const query = `
      SELECT provider, data_type as "dataType", last_sync_at as "lastSyncAt",
             last_sync_count as "lastSyncCount", sync_errors as "syncErrors"
      FROM sync_status 
      ORDER BY provider, data_type
    `;
    const result = await postgresClient.query(query);

    logger.info(`Retrieved sync status for ${result.rows.length} entries`);
    return result.rows;
  } catch (error) {
    logger.error('Failed to get sync status', { error });
    throw error;
  }
}
