class OneRoster::Terms::ClassesController < OneRoster::Controller
  include OneRoster::ClassroomScoped

  before_action :authorize_roster_scope
  before_action :validate_school_id, only: :index
  after_action :set_total_count_header, only: :index

  def index
    data = bind_and_parse(classrooms.map { |c| classroom_props(c) })
    render_success :ok, json: { classes: data }
  end

  private
    def term_id
      decode_source_id(params[:term_id])['id'].to_i
    end

    def classrooms
      current_school.classrooms
        .includes(:room, :class_subjects, :course, :periods)
        .by_semester(current_school, term_id)
        .by_type([:day_long, :period_long, :general])
        .to_a
    end

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