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

  def index
    data = temperatures
      .preload(:employee)
      .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 = []

    employee_ids = params[:temperatures].pluck(:employee_id)
    employees.where(id: employee_ids).each do |employee|
      temperature = employee.covid_temperatures.build(temperature_params)
      temperature.temperature = find_employee_temperature(employee.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 employee_count
    count = temperatures
      .by_date(params[:date])
      .distinct
      .count

    render_success :ok, json: { count: count, total: employees.by_status(true).count }
  end

  def employee_search
    data = employees
      .by_covid_temperature_submissions(params[:tracked]&.to_bool, params[:date])
      .ordered
      .map { |s| employee_props(s) }

    render_success :ok, json: data
  end

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

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

  private
    def temperatures
      current_school.covid_employee_temperatures
    end

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

    def employees
      current_school.employees
    end

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

    def find_employee_temperature(id)
      params[:temperatures].find { |t| t[:employee_id] == 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.employee.full_name(:reverse),
        temperature: temperature.temperature
      }
    end

    def employee_props(employee)
      {}.tap do |props|
        props[:id] = employee.id
        props[:full_name] = employee.full_name(:reverse)
        return props if params[:date].nil?

        props[:date] = params[:date]
        props[:tracked] = tracked_temperatures_by_employee_id.include?(employee.id)
      end
    end

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