//+------------------------------------------------------------------+
//|                                          SessionTablePrinter.mqh |
//+------------------------------------------------------------------+
#ifndef SESSIONTABLEPRINTER_MQH
#define SESSIONTABLEPRINTER_MQH

#include "SessionEnums.mqh"

//+------------------------------------------------------------------+
//| CSessionTablePrinter                                             |
//| Prints a formatted summary table of session metrics to the       |
//| Experts tab with fixed-width columns.                            |
//+------------------------------------------------------------------+
class CSessionTablePrinter
  {
private:
   string                PadRight(string value, int width) const;

public:
                     CSessionTablePrinter(void);
                    ~CSessionTablePrinter(void);

   void                  Print(const CSessionMetrics &metrics[], int count);
  };

//+------------------------------------------------------------------+
//| Constructor: no member state to initialize.                      |
//+------------------------------------------------------------------+
CSessionTablePrinter::CSessionTablePrinter(void)
  {
  }

//+------------------------------------------------------------------+
//| Destructor: no dynamic resources to release.                     |
//+------------------------------------------------------------------+
CSessionTablePrinter::~CSessionTablePrinter(void)
  {
  }

//+------------------------------------------------------------------+
//| Print                                                            |
//| Outputs a formatted table to the terminal log with columns for   |
//| session name, trade count, win rate, net P&L, and average hold   |
//| time, one row per session.                                       |
//+------------------------------------------------------------------+
void CSessionTablePrinter::Print(const CSessionMetrics &metrics[], int count)
  {
   string   header      = "";
   string   row         = "";
   double   win_rate    = 0.0;
//--- print the header row with fixed-width column labels
   header = PadRight("Session", 12) + PadRight("Trades", 10) +
            PadRight("Win Rate", 12) + PadRight("Net P&L", 14) +
            PadRight("Avg Hold(s)", 14);
   ::Print(header);
//--- print one row per session with the same fixed-width columns
   for(int i = 0; i < count; i++)
     {
      //--- guard against division by zero for sessions with no trades
      if(metrics[i].trade_count > 0)
         win_rate = ((double)metrics[i].win_count / (double)metrics[i].trade_count) * 100.0;
      else
         win_rate = 0.0;
      row = PadRight(metrics[i].name, 12) +
            PadRight(::IntegerToString(metrics[i].trade_count), 10) +
            PadRight(::DoubleToString(win_rate, 1) + "%", 12) +
            PadRight(::DoubleToString(metrics[i].net_pnl, 2), 14) +
            PadRight(::DoubleToString(metrics[i].average_hold_time, 1), 14);
      ::Print(row);
     }
  }

//+------------------------------------------------------------------+
//| PadRight                                                         |
//| Pads a string with trailing spaces up to the given width, or     |
//| returns the original string unchanged if it already meets or     |
//| exceeds that width.                                              |
//+------------------------------------------------------------------+
string CSessionTablePrinter::PadRight(string value, int width) const
  {
   int need = width - ::StringLen(value);
//--- return the value unchanged if it already fills the column
   if(need <= 0)
      return(value);
   string result = value;
   for(int i = 0; i < need; i++)
      result += " ";
   return(result);
  }

#endif // SESSIONTABLEPRINTER_MQH
//+------------------------------------------------------------------+