import { createTimeManager } from '../../utils/timeUtils.js';

export class BaseService {
  constructor(config) {
    this.config = config;
    this.mongodb = config.mongodb;
    this.redis = config.redis;
    this.logger = config.logger;
    this.timeManager = createTimeManager(config.timezone, config.dayStartsAt);
  }

  handleApiError(error, context = '') {
    this.logger.error(`API Error ${context}:`, {
      message: error.message,
      status: error.response?.status,
      data: error.response?.data,
    });

    if (error.response) {
      const status = error.response.status;
      const message = error.response.data?.message || error.response.statusText;

      if (status === 429) {
        throw new Error('API rate limit exceeded. Please try again later.');
      }

      throw new Error(`API error (${status}): ${message}`);
    }

    throw error;
  }
} 