class Admin::Covid::TemperaturesController < Admin::Covid::Controller
  include DateRangeHelper

  def index
    data = temperatures
      .includes(:student)
      .by_grades(params[:grades])
      .by_classroom(params[:classroom_ids])
      .by_date_range(datetime_range(params[:date_range]))

    render_success :ok, json: data.map { |d| temperature_props(d) }
  end

  def destroy
    if temperature.destroy
      render_success :ok
    else
      render_error :unprocessable_entity, errors: temperature
    end
  end

  def batch_create
    temperatures = []

    student_ids = params[:temperatures].pluck(:student_id)
    students.where(id: student_ids).each do |student|
      temperature = student.covid_temperatures.build(temperature_params)
      temperature.temperature = find_student_temperature(student.id)[:temperature]
      temperatures << temperature
    end

    if transactional_save(temperatures)
      render_success :ok, message: 'Temperatures saved.'
    else
      render_error :unprocessable_entity, message: 'Something went wrong'
    end
  end

  def student_count
    count = temperatures
      .by_date(params[:date])
      .distinct
      .pluck(:student_id)
      .count

    total = students.current_status(current_school, :current).count

    render_success :ok, json: { count: count, total: total }
  end

  def student_search
    data = students
      .preload(:classrooms)
      .current_status(current_school, :current)
      .by_grade(params[:grades])
      .by_covid_temperature_submissions(params[:tracked]&.to_bool, params[:date])
      .map { |s| student_props(s) }

    render_success :ok, json: data
  end

  def export
    Reporting::Covid::TemperatureJob.perform_async(
      current_school.id,
      current_user.id,
      date_range: params[:date_range],
      grades: params[:grades],
      classroom_ids: params[:classroom_ids]
    )
  end

  def google_export
    Google::Covid::TemperatureJob.perform_async(
      current_school.id,
      current_user.id,
      date_range: params[:date_range],
      grades: params[:grades],
      classroom_ids: params[:classroom_ids]
    )
  end

  private
    def temperature
      @temperature ||= temperatures.find_by(id: params[:id])
    end

    def temperatures
      current_school.covid_temperatures
    end

    def students
      current_school.students
    end

    def find_student_temperature(id)
      params[:temperatures].find { |t| t[:student_id] == id }
    end

    def tracked_temperatures_by_student_id
      @tracked_temperatures_by_student_id ||= temperatures.by_date(params[:date]).pluck(:student_id)
    end

    def temperature_params
      { recorded_by: current_user.full_name, recorded_at: date }
    end

    def date
      Time.zone.parse(params[:recorded_at])
    end

    def temperature_props(temperature)
      {
        id: temperature.id,
        recorded_at: temperature.recorded_at,
        full_name: temperature.student.full_name(:reverse),
        temperature: temperature.temperature
      }
    end

    def student_props(student)
      {}.tap do |props|
        props[:id] = student.id
        props[:full_name] = student.full_name(:reverse)
        props[:grade] = grade_label(student)
        props[:grade_id] = student.grade
        props[:classroom_ids] = student.classroom_ids
        return props if params[:date].nil?

        props[:date] = params[:date]
        props[:tracked] = tracked_temperatures_by_student_id.include?(student.id)
      end
    end

    def grade_label(student)
      grade_levels[student.grade]
    end

    def permissions
      ['write', 'manage']
    end
end
