import os
import csv


class Logger:

    def __init__(self):
        self.metrics = []
        self.directory = 'logs'
        self.fields = ['episode', 'step', 'agent', 'action', 'reward']

        with open(os.path.join(self.directory, "metrics.tsv"), "a", newline='') as csvfile:
            logwriter = csv.DictWriter(csvfile, delimiter='\t', fieldnames=self.fields)
            logwriter.writeheader()

    def log_metrics(self, metrics: dict):
        """
        :param metrics: dictionary containing 'episode', 'step', 'agent', 'action', 'reward' keys
        """
        with open(os.path.join(self.directory, "metrics.tsv"), "a", newline='') as csvfile:
            logwriter = csv.DictWriter(csvfile, delimiter='\t', fieldnames=self.fields)
            logwriter.writerow(metrics)
