class Admin::Legacy::Nursing::ComplaintsController < Admin::Legacy::Nursing::Controller
  def index
    render_success :ok, json: complaints.ordered.map { |c| complaint_props(c) }
  end

  def show
    render_success :ok, json: complaint_props(complaint)
  end

  def create
    complaint = complaints.build(complaint_params)

    if complaint.save
      render_success :ok, json: complaint_props(complaint)
    else
      render_error :unprocessable_entity, errors: complaint
    end
  end

  def update
    if complaint.update(complaint_params)
      render_success :ok, json: complaint_props(complaint)
    else
      render_error :unprocessable_entity, errors: complaint
    end
  end

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

  private
    def complaints
      current_school.nursing_complaints
    end

    def complaint
      @complaint ||= complaints.find_by(id: params[:id])
    end

    def complaint_params
      params.permit(:name)
    end

    def complaint_props(complaint)
      {
        id: complaint.id,
        name: complaint.name,
      }
    end
end
