class OneRoster::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

  def show
    render_success :ok, json: { class: classroom_props(classroom) }
  end

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

    def classroom
      current_school.send(model).find_by(id: id)
    end

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