package com.oguerreiro.resilient.activities.period.process;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.oguerreiro.resilient.activity.AbstractTask;
import com.oguerreiro.resilient.activity.GenericGuide;
import com.oguerreiro.resilient.activity.GenericResult;
import com.oguerreiro.resilient.activity.WorkResultEnum;
import com.oguerreiro.resilient.domain.Period;
import com.oguerreiro.resilient.domain.PeriodVersion;
import com.oguerreiro.resilient.service.OutputDataService;

@Qualifier("myClearOutputDataTask")
@Component
public class ClearOutputDataTaskImpl extends AbstractTask<GenericResult, GenericGuide<Period>, ClearOutputDataTask>
    implements ClearOutputDataTask {

  private final OutputDataService outputDataService;

  public ClearOutputDataTaskImpl(OutputDataService outputDataService) {
    this.outputDataService = outputDataService;
  }

  @Override
  public GenericResult doTask(GenericGuide<Period> guide) {
    PeriodVersion periodVersion = (PeriodVersion) guide.otherValues.get(
        PeriodProcessOutputsActivity.GUIDE_OTHERVALUE_PERIODVERSION);

    // Clear logs for InputdataUpload entity. With new transaction
    outputDataService.deleteAllByPeriodVersionInNewTransaction(periodVersion);

    return new GenericResult(WorkResultEnum.COMPLETED);
  }

}
