class StudentsController < ApplicationController
  # set_institution will try to load an institution when institution_id is present
  before_action :set_institution
  # set_student will safely find a student either scoped to @institution or globally
  before_action :set_student, only: %i[ show edit update destroy ]
  # ensure nested behaviour for new/create (these expect an institution). Allow index to show all students.
  before_action :ensure_institution_for_nested_actions, only: %i[new create]

  # GET /students or /students.json
  def index
    if @institution
      @students = @institution.students
    else
      # show all students when not nested
      @students = Student.all
    end
  end

  # GET /students/1 or /students/1.json
  def show
  end

  # GET /students/new
  def new
    @student = @institution.students.build
  end

  # GET /students/1/edit
  def edit
  end

  # POST /students or /students.json
  # def create
  #   @student = @institution.students.build(student_params)
  #   if @student.save
  #     redirect_to institution_students_path(@institution), notice: 'Student was successfully created.'
  #   else
  #     render :new, status: :unprocessable_entity
  #   end
  # end
  def create
    @student = @institution.students.new(student_params)

    respond_to do |format|
      if @student.save
        format.html { redirect_to institution_students_path(@institution), notice: 'Student was successfully created.' }
        format.js   # renders create.js.erb
      else
        format.html { render :new, status: :unprocessable_entity }
        format.js { render :error }
      end
    end
  end


  # PATCH/PUT /students/1 or /students/1.json
  def update
    if @student.update(student_params)
      redirect_to institution_students_path(@institution), notice: 'Student was successfully updated.'
    else
      render :edit, status: :unprocessable_entity
    end
  end

  # DELETE /students/1 or /students/1.json
  def destroy
    @student.destroy
    redirect_to institution_students_path(@institution), notice: 'Student was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_institution
      return unless params[:institution_id].present?
      @institution = Institution.find(params[:institution_id])
    rescue ActiveRecord::RecordNotFound
      # Let other parts handle missing institution (redirects in actions)
      @institution = nil
    end

    def set_student
      if @institution.present?
        @student = @institution.students.find(params[:id])
      else
        # fallback to global lookup when not nested
        @student = Student.find(params[:id]) if params[:id].present?
        @institution = @student.institution if @student.present?
      end
    rescue ActiveRecord::RecordNotFound
      # will raise in actions if record missing
      raise
    end

    def ensure_institution_for_nested_actions
      return if @institution.present?
      redirect_to institutions_path, alert: 'Please select an Institution first.'
    end

    # Only allow a list of trusted parameters through.
    def student_params
      params.require(:student).permit(:first_name, :last_name, :email, :institution_id, :place, :phone_number, :age, :photo)
    end
end
