class Admin::Legacy::Student::Students::GraduationPlansController <
  Admin::Legacy::Student::Controller
  include EdFi::IndianaHelper

  def index
    render_success :ok, json: graduation_plans.map { |g| graduation_props(g) }
  end

  def create
    graduation_plan = graduation_plans.build(graduation_plan_params)

    if graduation_plan.save
      render_success :ok, json: graduation_props(graduation_plan)
    else
      render_error :unprocessable_entity, errors: graduation_plan
    end
  end

  def update
    if graduation_plan.update(graduation_plan_params)
      render_success :ok, json: graduation_props(graduation_plan)
    else
      render_error :unprocessable_entity, errors: graduation_plan
    end
  end

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

  private
    def graduation_plans
      student.ed_fi_graduation_plans
    end

    def graduation_plan
      @graduation_plan ||= graduation_plans.find_by(id: params[:id])
    end

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

    def graduation_plan_params
      params.permit(
        :plan_descriptor,
        :alternative_plan_descriptor,
        :plan_descriptor_name,
        :alternative_plan_descriptor_name,
        :start_date,
        :end_date
      )
    end

    def graduation_props(graduation_plan)
      {}.tap do |props|
        props[:id] = graduation_plan.id
        props[:start_date] = graduation_plan.start_date
        props[:end_date] = graduation_plan.end_date
        props[:plan_descriptor] = graduation_plan.plan_descriptor
        props[:alternative_plan_descriptor] = graduation_plan.alternative_plan_descriptor
        props[:plan_descriptor_name] = graduation_plan.plan_descriptor_name
        props[:alternative_plan_descriptor_name] = graduation_plan.alternative_plan_descriptor_name
      end
    end
end
