class Admin::Legacy::Nursing::PrescriptionDistributionsController <
    Admin::Legacy::Nursing::Controller
  def index
    render_success :ok, json: prescriptions.by_date(params[:date]).map { |p| prescription_props(p) }
  end

  def show
    render_success :ok, json: prescription_distribution_props(prescription_distribution)
  end

  def create
    prescription_distribution = prescription_distributions.build(prescription_distribution_params)

    if prescription_distribution.save
      render_success :ok, message: 'Prescription given.'
    else
      render_error :unprocessable_entity, errors: prescription_distribution
    end
  end

  def update
    if prescription_distribution.update(prescription_distribution_params)
      render_success :ok
    else
      render_error :unprocessable_entity, errors: prescription_distribution
    end
  end

  def destroy
    if prescription_distribution.destroy
      render_success :ok, message: 'Prescription record deleted.'
    else
      render_error :unprocessable_entity, errors: prescription_distribution
    end
  end

  private
    def prescription_distributions
      current_school.nursing_prescription_distributions
    end

    def prescription_distribution
      @prescription_distribution ||= prescription_distributions.find_by(id: params[:id])
    end

    def prescription_distribution_params
      params.permit(:time, :date).merge(prescription_associations)
    end

    def prescriptions
      current_school.nursing_prescriptions
        .includes(:student)
        .where(student_id: current_student_ids)
    end

    def prescription
      @prescription ||= prescriptions.find_by(id: params[:prescription_id])
    end

    def student
      @student ||= current_school.students.find_by(id: params[:student_id])
    end

    def current_student_ids
      @current_student_ids ||= current_school_year.school_year_students
        .where(current: true)
        .pluck(:student_id)
    end

    def prescription_associations
      return {} unless action_name.to_sym == :create

      {
        student: student,
        prescription: prescription
      }
    end

    def prescription_distributions_hash
      @prescription_distributions_hash ||= prescription_distributions.by_date(params[:date])
    end

    def prescription_distribution_props(item)
      {
        id: item.id,
        date: item.date,
        time: item.time
      }
    end

    def prescription_props(prescription)
      item = prescription_distributions_hash.find { |s| s.prescription_id == prescription.id }

      {}.tap do |prop|
        prop[:id] = item&.id
        prop[:time] = item&.time&.strftime('%H:%M')
        prop[:student_id] = prescription.student_id
        prop[:prescription_id] = prescription.id
        prop[:medication] = prescription.medication
        prop[:student_name] = prescription&.student&.full_name(:reverse)
        prop[:dose] = prescription.dose
        prop[:route] = prescription.route
        prop[:self_administer] = prescription.self_administer?
        prop[:start_date] = prescription.start_date
        prop[:stop_date] = prescription.stop_date
        prop[:schedule] = prescription.schedule
        prop[:notes] = prescription.notes
      end
    end
end
