// backend/src/services/AuthLogger.ts
import * as fs from 'fs';
import * as path from 'path';

const LOG_FILE_PATH = path.join(__dirname, '../../logs/auth.log');

export class AuthLogger {
  private static ensureLogFileExists() {
    const logDir = path.dirname(LOG_FILE_PATH);
    if (!fs.existsSync(logDir)) {
      fs.mkdirSync(logDir, { recursive: true });
    }
    if (!fs.existsSync(LOG_FILE_PATH)) {
      fs.writeFileSync(LOG_FILE_PATH, '', { encoding: 'utf8' });
    }
  }

  public static logAttempt(
    type: 'success' | 'failure',
    ipAddress: string,
    timestamp: Date = new Date()
  ): void {
    AuthLogger.ensureLogFileExists();
    const logEntry = `${timestamp.toISOString()} - ${type.toUpperCase()} - IP: ${ipAddress}\n`;
    fs.appendFileSync(LOG_FILE_PATH, logEntry, { encoding: 'utf8' });
  }
}
