class Api::V1::Projects::Actions::Runs::JobShowService < ApplicationService 
  include ActiveModel::Model

  attr_reader :project, :token, :owner, :repo, :run, :job, :log_cursors
  attr_accessor :gitea_data 

  validates :run, :job, :log_cursors, presence: true 

  def initialize(project, run, job, log_cursors, token = nil)
    @project = project
    @owner = project&.owner.login 
    @repo = project&.identifier
    @run = run 
    @job = job 
    @log_cursors = log_cursors
    @token = token
  end

  def call 
    raise Error, errors.full_messages.join(",") unless valid? 
    load_gitea_data 

    @gitea_data
  end

  private 
  def request_params 
    {
      access_token: token
    }
  end

  def request_body
    {
      logCursors: log_cursors
    }
  end

  def load_gitea_data
    @gitea_data = $gitea_hat_client.post_repos_actions_runs_jobs_by_owner_repo_run_job(owner, repo, run, job, {query: request_params, body: request_body.to_json})
  end
end