// Import bash clear command
import clear from 'clear';

// Import shared types
import Deployment from '../types/Deployment';

// Import shared helpers
import exec from '../helpers/exec';
import print from '../helpers/print';

/**
 * View the tail of the logs
 * @author Gabe Abrams
 */
const viewLogs = async (deployment: Deployment) => {
  // Clear the screen
  clear();

  // Get the start of the day
  const now = new Date();
  const startOfDayDate = new Date(`${now.getMonth() + 1}/${now.getDate()}/${now.getFullYear()}`);
  const startOfDayISO = startOfDayDate.toISOString();

  // Show start of logs
  print.title(`Logs Begin Here for ${now.getMonth() + 1}/${now.getDate()}/${now.getFullYear()}`);

  // Show the logs
  exec(
    `aws logs tail /CacclDeploy-${deployment.app}/app --since ${startOfDayISO} --profile ${deployment.profile}`,
    true,
  );

  // Wait for user to exit
  print.title('Scroll Up for Logs');
  await print.enterToContinue();
};

export default viewLogs;
