class Admin::StateReporting::DashboardController < Admin::StateReporting::Controller
  def show
    render_success :ok, json: props
  end

  def sync_calendars
    if config.sync_calendars
      render_success :ok, message: 'Sync Started.'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_students
    if config.sync_students
      render_success :ok, message: 'Sync Started.'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_master_schedule
    if config.sync_master_schedule
      render_success :ok, message: 'Sync Started.'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_attendance
    if config.sync_attendance
      render_success :ok, message: 'Sync Started.'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_parents
    if config.sync_parents
      render_success :ok, message: 'Sync Started.'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_staff
    if config.sync_staff
      render_success :ok, message: 'Sync Started.'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_student_education
    if config.sync_student_education
      render_success :ok, message: 'Sync started.'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_student_programs
    if config.sync_student_programs
      render_success :ok, message: 'Sync Started'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_course_offerings
    if config.sync_course_offerings
      render_success :ok, message: 'Sync Started'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_student_transcripts
    if config.sync_student_transcripts(params[:grade])
      render_success :ok, message: 'Sync Started'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_course_section_rosters
    if config.sync_course_section_rosters
      render_success :ok, message: 'Sync Started'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_student_accommodations
    if config.sync_student_accommodations
      render_success :ok, message: 'Sync Started'
    else
      render_error :unprocessable_entity
    end
  end

  def sync_student_graduation_plans
    if config.sync_student_graduation_plans
      render_success :ok, message: 'Sync Started'
    else
      render_error :unprocessable_entity
    end
  end

  def staff_report
    Reporting::EdFi::Indiana::StaffJob.perform_async(current_school.id, current_user.id)
  end

  def students_report
    Reporting::EdFi::Indiana::StudentsJob.perform_async(current_school.id, current_user.id)
  end

  def transcript_report
    Reporting::EdFi::Indiana::TranscriptJob.perform_async(
      current_school.id,
      current_user.id,
      grade: params[:grade]
    )
  end

  def graduate_report
    Reporting::EdFi::Indiana::GraduateJob.perform_async(
      current_school.id,
      current_user.id
    )
  end

  def programs_report
    Reporting::EdFi::Indiana::ProgramsJob.perform_async(current_school.id, current_user.id)
  end

  def attendance_report
    Reporting::EdFi::Indiana::AttendanceJob.perform_async(current_school.id, current_user.id)
  end

  def courses_report
    Reporting::EdFi::Indiana::CoursesJob.perform_async(current_school.id, current_user.id)
  end

  def accommodations_report
    Reporting::EdFi::Indiana::AccommodationsJob.perform_async(current_school.id, current_user.id)
  end

  def graduation_plans_report
    Reporting::EdFi::Indiana::GraduationPlansJob.perform_async(current_school.id, current_user.id)
  end

  private
    def school_days
      @school_days ||= current_school_year.school_days
        .where.not(day_descriptor_id: nil)
        .exists?
    end

    def permissions
      perms = [:index, :show, :available, :students_report, :staff_report, :transcript_report]
      if perms.include?(action_name.to_sym)
        ['read', 'write', 'manage']
      else
        ['write', 'manage']
      end
    end

    def config
      @config ||= current_school_year.find_or_build_state_reporting_config
    end

    def grades_mapped?
      current_school.grades.find_by(edfi_grade: nil).nil?
    end

    def error_logs
      current_school.ed_fi_logs.by_school_year(config.school_year_id)
    end

    def indiana_service
      dir = "#{current_school.indiana_environment_service}::ApplicationService"
      dir.constantize.new(config.school_year_id)
    end

    def props
      {
        enabled: config.enabled,
        school_year_id: config.school_year_id,
        name: current_school_year.name,
        valid_credentials: indiana_service.valid_token?,
        state_id: indiana_service.edfi_school?,
        valid_grades: grades_mapped?,
        has_errors: error_logs.exists?,
        calendar_syncable: school_days
      }
    end
end
