class Api::Pm::DashboardsController <  Api::Pm::BaseController
  before_action :require_login
  def index 
  end

  def todo 
    return render_error('请输入正确的pm_project_ids.') if params[:pm_project_ids].blank? 
    pm_project_ids = params[:pm_project_ids].split(",") rescue []
    date = params[:date].present? ? params[:date].to_date : Date.today rescue Date.today
    @issues = Issue.where("start_date <= ? and due_date >= ?", date, date)
    @issues = @issues.where(pm_project_id: pm_project_ids).joins(:issue_participants).where(issue_participants: {participant_id: current_user.id, participant_type: 'assigned'})
    @issues = @issues.where.not(status_id: 5)
    @issues = kaminari_paginate(@issues.distinct.pm_includes)
  end

  def my_issues
    return render_error('请输入正确的pm_project_ids.') if params[:pm_project_ids].blank? 
    return render_error('请输入正确的pm_issue_types.') if params[:pm_issue_types].blank?
    pm_project_ids = params[:pm_project_ids].split(",") rescue []
    pm_issue_types = params[:pm_issue_types].split(",") rescue []
    @all_issues = Issue.where(pm_project_id: pm_project_ids, pm_issue_type: pm_issue_types).order(created_on: :desc)
    @issues = @all_issues.joins(:issue_participants).where(issue_participants: {participant_id: current_user.id, participant_type: 'assigned'})
    @issues = @issues.where.not(status_id: 5) if params[:category] == "opened"
    @issues = kaminari_paginate(@issues.distinct.pm_includes)
    @my_assign_requirements_count = @all_issues.where(pm_issue_type: 1).joins(:issue_participants).where(issue_participants: {participant_id: current_user.id, participant_type: 'assigned'}).size
    @my_assign_tasks_count = @all_issues.where(pm_issue_type: 2).joins(:issue_participants).where(issue_participants: {participant_id: current_user.id, participant_type: 'assigned'}).size
    @my_assign_bugs_count = @all_issues.where(pm_issue_type: 3).joins(:issue_participants).where(issue_participants: {participant_id: current_user.id, participant_type: 'assigned'}).size
  end

  def my_pm_projects
    return render_error('请输入正确的pm_project_id.') if params[:pm_project_id].blank? 
    @all_issues = Issue.where(pm_project_id: params[:pm_project_id])
    time_now = Time.now
    @last_week_create_issues_count = @all_issues.where("created_on > ? and created_on < ?", time_now - 7.days, time_now).size 
    @before_last_week_create_issue_count = @all_issues.where("created_on > ? and created_on < ?", time_now - 14.days, time_now - 7.days).size
    @compare_last_week_create_issues = @before_last_week_create_issue_count.zero? ? 0 :(@last_week_create_issues_count - @before_last_week_create_issue_count).to_f / @before_last_week_create_issue_count rescue 0
    @last_week_close_issues_count = @all_issues.where(status_id: 5).where("updated_on > ? and updated_on < ?", time_now - 7.days, time_now).size
    @before_last_week_close_issue_count = @all_issues.where(status_id: 5).where("updated_on > ? and updated_on < ?", time_now - 14.days, time_now - 7.days).size
    @compare_last_week_close_issues = @before_last_week_close_issue_count.zero? ? 0 :(@last_week_close_issues_count - @before_last_week_close_issue_count).to_f / @before_last_week_close_issue_count rescue 0
    @all_requirement_issues_count = @all_issues.where(pm_issue_type: 1).size
    @open_requirement_issues_count = @all_issues.where(pm_issue_type: 1).where.not(status_id: 5).size
    @last_week_close_requirement_issues_count = @all_issues.where(pm_issue_type: 1).where(status_id: 5).where("updated_on > ? and updated_on < ?", time_now - 7.days, time_now).size
    @last_month_close_requirement_issues_count = @all_issues.where(pm_issue_type: 1).where(status_id: 5).where("updated_on > ? and updated_on < ?", time_now - 30.days, time_now).size
    @all_task_issues_count = @all_issues.where(pm_issue_type: 2).size 
    @open_task_issues_count = @all_issues.where(pm_issue_type: 2).where.not(status_id: 5).size
    @last_week_close_tast_issues_count = @all_issues.where(pm_issue_type: 2).where(status_id: 5).where("updated_on > ? and updated_on < ?", time_now - 7.days, time_now).size
    @last_month_close_task_issues_count = @all_issues.where(pm_issue_type: 2).where(status_id: 5).where("updated_on > ? and updated_on < ?", time_now - 30.days, time_now).size
    @all_bug_issues_count = @all_issues.where(pm_issue_type: 3).size
    @open_bug_issues_count = @all_issues.where(pm_issue_type: 3).where.not(status_id: 5).size
    @last_week_close_bug_issues_count = @all_issues.where(pm_issue_type: 3).where(status_id: 5).where("updated_on > ? and updated_on < ?", time_now - 7.days, time_now).size
    @last_month_close_bug_issues_count = @all_issues.where(pm_issue_type: 3).where(status_id: 5).where("updated_on > ? and updated_on < ?", time_now - 30.days, time_now).size
    

    @requirement_close_trend = [[],[]]
    @task_close_trend = [[],[]]
    @bug_close_trend = [[],[]]
    ((time_now-29.days).to_date..time_now.to_date).to_a.each do |i|
      @requirement_close_trend[0] << i.strftime("%Y.%m.%d")
      @task_close_trend[0] << i.strftime("%Y.%m.%d")
      @bug_close_trend[0] << i.strftime("%Y.%m.%d") 
      @requirement_close_trend[1] << @all_issues.where(pm_issue_type: 1, status_id: 5).where("DATE(updated_on) = ?", i).size
      @task_close_trend[1] << @all_issues.where(pm_issue_type: 2, status_id: 5).where("DATE(updated_on) = ?", i).size
      @bug_close_trend[1] << @all_issues.where(pm_issue_type: 3, status_id: 5).where("DATE(updated_on) = ?", i).size
    end
    @close_trend = {requirement: @requirement_close_trend, task: @task_close_trend, bug: @bug_close_trend}

    render_ok(data: {
      last_week_close_issues_count: @last_week_close_issues_count,
      before_last_week_close_issue_count: @before_last_week_close_issue_count,
      compare_last_week_close_issues: @compare_last_week_close_issues,
      last_week_create_issues_count: @last_week_create_issues_count,
      before_last_week_create_issue_count: @before_last_week_create_issue_count,
      compare_last_week_create_issues: @compare_last_week_create_issues,
      all_requirement_issues_count: @all_requirement_issues_count,
      open_requirement_issues_count: @open_requirement_issues_count,
      last_week_close_requirement_issues_count: @last_week_close_requirement_issues_count,
      last_month_close_requirement_issues_count: @last_month_close_requirement_issues_count,
      all_task_issues_count: @all_task_issues_count,
      open_task_issues_count: @open_task_issues_count,
      last_week_close_task_issues_count: @last_week_close_tast_issues_count,
      last_month_close_task_issues_count: @last_month_close_task_issues_count,
      all_bug_issues_count: @all_bug_issues_count,
      open_bug_issues_count: @open_bug_issues_count,
      last_week_close_bug_issues_count: @last_week_close_bug_issues_count,
      last_month_close_bug_issues_count: @last_month_close_bug_issues_count,
      close_trend: @close_trend
    })
  end

  def my_projects
    return render_error('请输入正确的project_id.') if params[:project_id].blank?
    
    @project = Project.find_by_id params[:project_id]
    return render_error('请输入正确的project_id.') unless @project.present?
    time_now = Time.now

    branch_tag_result = $gitea_hat_client.get_repos_branch_tag_count_by_owner_repo(@project&.owner&.login, @project&.identifier) rescue {}
    languages_result = $gitea_client.get_repos_languages_by_owner_repo(@project&.owner&.login, @project&.identifier) rescue {}

    @open_pull_requests_count = @project.pull_requests.opening.size 
    @last_week_close_pull_requests_count = @project.pull_requests.where(status: 1).where("updated_at > ? and updated_at < ?", time_now - 7.days, time_now).size
    @last_month_close_pull_requets_count = @project.pull_requests.where(status: 1).where("updated_at > ? and updated_at < ?", time_now - 30.days, time_now).size

    @commits_count = @project.commit_logs.size
    @last_week_commits_count = @project.commit_logs.where("created_at > ? and created_at < ?", time_now - 7.days, time_now).size
    @last_month_commits_count = @project.commit_logs.where("created_at > ? and created_at < ?", time_now - 30.days, time_now).size

    render_ok(data: {
      branch_count: branch_tag_result["branch_count"].to_i,
      tag_count: branch_tag_result["tag_count"].to_i,
      license_name: @project.license&.name,
      open_pull_requests_count: @open_pull_requests_count,
      last_week_close_pull_requests_count: @last_week_close_pull_requests_count,
      last_month_close_pull_requets_count: @last_month_close_pull_requets_count,
      commits_count: @commits_count,
      last_week_commits_count: @last_week_commits_count,
      last_month_commits_count: @last_month_commits_count,
      language: hash_transform_precentagable(languages_result),
    })
  end

  def my_operate_journals
    return render_error('请输入正确的pm_project_id.') if params[:pm_project_id].blank? 
    @journals = Journal.operate_journals.joins(:issue).where(issues: {pm_project_id: params[:pm_project_id], pm_issue_type: [1,2,3]})

    @journals = kaminari_paginate(@journals.order(updated_on: :desc))
  end

  private 
  def hash_transform_precentagable(hash)
    total_byte_size = hash.values.sum
    hash.transform_values { |v|
      ActionController::Base.helpers
        .number_to_percentage((v * 100.0 / total_byte_size), precision: 1)
    }.select{|k,v| v != "0.0%"}
  end
end