class Admins::DashboardsController < Admins::BaseController
  before_action :require_business

  def index
    # 用户活跃数
    day_user_ids = CommitLog.where(created_at: today).pluck(:user_id).uniq
    weekly_user_ids = CommitLog.where(created_at: current_week).pluck(:user_id).uniq
    month_user_ids = CommitLog.where(created_at: current_month).pluck(:user_id).uniq
    @active_user_count = User.where(last_login_on: today).or(User.where(id: day_user_ids)).count
    @weekly_active_user_count = User.where(last_login_on: current_week).or(User.where(id: weekly_user_ids)).count
    @month_active_user_count = User.where(last_login_on: current_month).or(User.where(id: month_user_ids)).count
    user_ids = User.where(created_on: pre_week).pluck(:id).uniq
    weekly_keep_user_count = User.where(id: user_ids).where(last_login_on: current_week).count
    @weekly_keep_rate = format("%.2f", user_ids.size > 0 ? weekly_keep_user_count.to_f / user_ids.size : 0)

    # 新用户注册数
    @day_new_user_count = User.where(created_on: today).count
    @weekly_new_user_count = User.where(created_on: current_week).count
    @month_new_user_count = User.where(created_on: current_month).count

    # 活跃项目数
    day_project_ids = (CommitLog.where(created_at: today).pluck(:project_id).uniq + Issue.where(created_on: today).pluck(:project_id).uniq).uniq
    weekly_project_ids = (CommitLog.where(created_at: current_week).pluck(:project_id).uniq + Issue.where(created_on: current_week).pluck(:project_id).uniq).uniq
    month_project_ids = (CommitLog.where(created_at: current_month).pluck(:project_id).uniq + Issue.where(created_on: current_month).pluck(:project_id).uniq).uniq
    @day_active_project_count = Project.where(updated_on: today).or(Project.where(id: day_project_ids)).count
    @weekly_active_project_count = Rails.cache.fetch("dashboardscontroller:weekly_active_project_count", expires_in: 10.minutes) do
      Project.where(updated_on: current_week).or(Project.where(id: weekly_project_ids)).count
    end
    @month_active_project_count = Rails.cache.fetch("dashboardscontroller:month_active_project_count", expires_in: 1.hours) do
      Project.where(updated_on: current_month).or(Project.where(id: month_project_ids)).count
    end
    # 新增项目数
    @day_new_project_count = Rails.cache.fetch("dashboardscontroller:day_new_project_count", expires_in: 10.minutes) do
      Project.where(created_on: today).count
    end
    @weekly_new_project_count = Rails.cache.fetch("dashboardscontroller:weekly_new_project_count", expires_in: 10.minutes) do
      Project.where(created_on: current_week).count
    end
    @month_new_project_count = Rails.cache.fetch("dashboardscontroller:month_new_project_count", expires_in: 1.hours) do
      Project.where(created_on: current_month).count
    end


    # 总的平台用户数
    # 总的平台项目数
    # 总的平台组织数
    # 总的平台Issue数、评论数、PR数、Commit数
    @user_count = Rails.cache.fetch("dashboardscontroller:platform:user_count", expires_in: 1.days) do
      User.count
    end
    @project_count = Rails.cache.fetch("dashboardscontroller:platform:project_count", expires_in: 1.days) do
      Project.count
    end
    @organization_count = Rails.cache.fetch("dashboardscontroller:platform:organization_count", expires_in: 1.days) do
      Organization.count
    end
    @issue_count = Rails.cache.fetch("dashboardscontroller:platform:issue_count", expires_in: 1.days) do
      Issue.count
    end
    @comment_count = Rails.cache.fetch("dashboardscontroller:platform:comment_count", expires_in: 1.days) do
      Journal.count
    end
    @pr_count = Rails.cache.fetch("dashboardscontroller:platform:pr_count", expires_in: 1.days) do
      PullRequest.count
    end
    @commit_count = Rails.cache.fetch("dashboardscontroller:platform:commit_count", expires_in: 1.days) do
      CommitLog.count
    end

    @subject_name = ["用户数", "项目数", "组织数", "Issue数", "Issue评论数", "PR数", "Commit数", "在线用户数"]
    @subject_icon = ["fa-user","fa-git", "fa-sitemap", "fa-warning", "fa-comments", "fa-share-alt", "fa-upload", "fa-user"]
    @subject_data = [@user_count, @project_count, @organization_count, @issue_count, @comment_count, @pr_count, @commit_count, UserOnline.count]

    if EduSetting.get("open_baidu_tongji").to_s == "true"
      tongji_service = Baidu::TongjiService.new
      @access_token = tongji_service.access_token
      Rails.logger.info "baidu_tongji_auth access_token ===== #{@access_token}"
      # @overview_data = tongji_service.api_overview
      start_date = DailyPlatformStatistic.order(:date).last.try(:date) || Time.now.to_date
      end_date = Time.now
      if @access_token.present?
        @overview_data = Rails.cache.fetch("dashboardscontroller:baidu_tongji:overview_data", expires_in: 10.minutes) do
          tongji_service.source_from_batch_add(start_date, end_date)
          @overview_data = tongji_service.overview_batch_add(start_date, end_date)
          @overview_data
        end
      end

      @current_week_statistic = DailyPlatformStatistic.where(date: current_week)
      @pre_week_statistic = DailyPlatformStatistic.where(date: pre_week)
    end


  end

  def month_active_user
    count = UserExtension.where(created_at: current_month).group(:identity).count

    data = [
      { value: count['teacher'].to_i, name: '老师' },
      { value: count['student'].to_i, name: '学生' },
      { value: count['professional'].to_i, name: '专业人士' },
      { value: count[nil].to_i, name: '未选职业' },
    ]
    render_ok(data: data)
  end

  def baidu_tongji
    tongji_service = Baidu::TongjiService.new
    redirect_to tongji_service.code_url
  end

  def baidu_tongji_auth
    if params[:code].present?
      tongji_service = Baidu::TongjiService.new
      tongji_service.get_access_token(params[:code])
    end
    redirect_to "/admins/"
  end

  def evaluate
    names = []
    data = []

    1.upto(7) do |i|
      date = i.days.ago
      names.unshift(date.strftime('%Y-%m-%d'))

      count = Output.where(created_at: date.beginning_of_day..date.end_of_day).count
      data.unshift(count)
    end

    render_ok(names: names, data: data)
  end

  private

  def today
    Time.now.beginning_of_day..Time.now.end_of_day
  end

  def pre_7_days
    7.days.ago.end_of_day..Time.now.end_of_day
    end

  def current_week
    Time.now.beginning_of_week..Time.now.end_of_day
  end

  def current_month
    30.days.ago.end_of_day..Time.now.end_of_day
  end

  def pre_week
    # 14.days.ago.end_of_day..7.days.ago.end_of_day
    Time.now.prev_week..Time.now.prev_week.end_of_week
  end
end