import json, io

from services.base import Base
from helpers import aws_helper
from models.ability_estimation import AbilityEstimation
from lib.application_configs import ApplicationConfigs

class AbilityEstimationService(Base):
  ACTION = 'abilityEstimation'

  def process(self):
    attributes = self.service_attributes()
    ability_estimation = AbilityEstimation.parse_obj(attributes)
    result = ability_estimation.calculate()

    if result is not None:
      response = json.dumps({
        'status': 'success',
        'result': result
      })

      self.file_name = f'{ability_estimation.exam_id}_ability_estimation_result.json'
    else:
      response = json.dumps({
        'status': 'error',
        'result': None
      })

    aws_helper.file_stream_upload(
        io.BytesIO(bytes(response.encode('UTF-8'))), self.file_name,
        ApplicationConfigs.s3_processed_bucket, self.ACTION)