Gaku::Core::Engine.routes.draw do
  # concerns

  concern :addresses do
    resources :addresses, concerns: %i[soft_delete primary], except: %i[show index soft]
  end

  concern :contacts do
    resources :contacts, concerns: %i[soft_delete primary]
  end

  concern :notes do
    resources :notes
  end

  concern :soft_delete do
    patch :recovery,    on: :member
    patch :soft_delete, on: :member
  end

  concern(:primary)         { patch :make_primary, on: :member }
  concern(:show_deleted)    { get :show_deleted, on: :member }
  concern(:pagination)      { get 'page/:page', action: :index, on: :collection }
  concern(:sort)            { post :sort, on: :collection }
  concern(:download)        { get :download, on: :member }

  concern(:enroll_students) { post :enroll_students, on: :collection }
  concern(:enroll_student)  { post :enroll_student, on: :collection }
  concern(:student_chooser) { get :student_chooser, on: :member }

  # devise
  devise_for :users,
             class_name: 'Gaku::User',
             module: :devise,
             controllers: {
               sessions: 'gaku/devise/sessions',
               registrations: 'gaku/devise/registrations',
               passwords: 'gaku/devise/passwords'
             }

  devise_scope :user do
    get :set_up_admin_account, to: 'devise/registrations#set_up_admin_account'
    post :create_admin,        to: 'devise/registrations#create_admin'
  end

  resources :extracurricular_activities, concerns: %i[student_chooser pagination soft_delete show_deleted] do
    resources :students,
              controller: 'extracurricular_activities/students',
              concerns: %i[enroll_student]
  end

  resources :class_groups, concerns: %i[notes soft_delete student_chooser] do
    resources :semester_class_groups, controller: 'class_groups/semester_class_groups'
    resources :class_group_course_enrollments, controller: 'class_groups/courses', only: %i[new create destroy]
    resources :students, controller: 'class_groups/students', only: %i[new destroy], concerns: %i[enroll_student]
  end

  resources :courses, concerns: %i[notes student_chooser soft_delete show_deleted] do
    resources :semester_courses, controller: 'courses/semester_courses'
    resources :enrollments, controller: 'courses/enrollments', concerns: %i[enroll_student] do
      post :enroll_class_group, on: :collection
    end

    resources :exams, controller: 'courses/exams' do
      resources :exam_portion_scores, controller: 'courses/exams/exam_portion_scores' do
        resources :attendances
      end

      collection do
        get :grading
        get :export
      end

      member do
        get :grading
        put :update_score
        get :completed
      end
    end
  end

  resources :class_group_enrollments,              concerns: %i[enroll_students]
  resources :course_enrollments,                   concerns: %i[enroll_students]
  resources :extracurricular_activity_enrollments, concerns: %i[enroll_students]

  resources :syllabuses, concerns: %i[notes soft_delete show_deleted] do
    resources :assignments,     controller: 'syllabuses/assignments'
    resources :exams,           controller: 'syllabuses/exams'
    resources :exam_syllabuses, controller: 'syllabuses/exam_syllabuses'
  end

  resources :teachers, concerns: %i[addresses contacts notes soft_delete show_deleted pagination]

  resources :students, concerns: %i[addresses contacts notes soft_delete show_deleted pagination] do
    get :load_autocomplete_data, on: :collection

    resources :simple_grades,        controller: 'students/simple_grades', except: :show
    resources :commute_methods,      controller: 'students/commute_methods'
    resources :student_achievements, controller: 'students/student_achievements', except: :show
    resources :student_specialties,  controller: 'students/student_specialties',  except: :show

    resources :guardians, except: %i[index show],
                          controller: 'students/guardians',
                          concerns: %i[addresses contacts soft_delete]

    resources :course_enrollments,
              controller: 'students/course_enrollments',
              only: %i[new create destroy]

    resources :class_group_enrollments, controller: 'students/class_group_enrollments'
  end

  resources :exams, concerns: %i[notes soft_delete] do
    put :create_exam_portion, on: :member

    resources :exam_scores
    resources :exam_portions, controller: 'exams/exam_portions', concerns: %i[sort] do
      resources :attachments, controller: 'exams/exam_portions/attachments'
    end
  end

  resources :states, only: :index

  root to: 'home#index'
end
