module OneRoster::AssignmentScoped
  extend ActiveSupport::Concern
  include OneRoster::ClassroomScoped
  include OneRoster::CategoryScoped
  include OneRoster::AcademicSessionScoped

  def index
    data = bind_and_parse(assignments.map { |a| assignment_props(a) })
    render_success :ok, json: { assignments: data }
  end

  def show
    render_success :ok, json: { assignment: assignment_props(assignment) }
  end

  private
    def set_total_count_header
      response.set_header('X-Total-Count', assignments.count.to_s)
    end

    def assignment_props(assignment)
      {
        sourceId: encode_source_id(:class_assignments, assignment.id),
        status: :active,
        dateLastModified: assignment.updated_at.iso8601,
        title: assignment.name,
        description: assignment.description,
        assignDate: assignment.due_date,
        dueDate: assignment.due_date,
        class: classroom_references(assignment.classroom),
        category: category_references(assignment.class_grade_category_id),
        gradingPeriod: session_references(assignment.quarter, current_school_year, 'gradingPeriod'),
        resultValueMin: 0,
        resultValueMax: assignment.possible
      }
    end
end
