# == Schema Information
#
# Table name: issue_tags
#
#  id                  :integer          not null, primary key
#  name                :string(190)
#  description         :string(255)
#  color               :string(255)
#  user_id             :integer
#  project_id          :integer
#  issues_count        :integer          default("0")
#  created_at          :datetime         not null
#  updated_at          :datetime         not null
#  gid                 :integer
#  gitea_url           :string(255)
#  pull_requests_count :integer          default("0")
#  pm_project_id       :integer
#  organization_id     :integer
#
# Indexes
#
#  index_issue_tags_on_organization_id                  (organization_id)
#  index_issue_tags_on_user_id_and_name_and_project_id  (user_id,name,project_id)
#

class IssueTag < ApplicationRecord

  has_many :issue_tags_relates, dependent: :destroy
  has_many :issues, through: :issue_tags_relates
  has_many :issue_issues, -> {where(issue_classify: [nil,"issue"])}, source: :issue, through: :issue_tags_relates
  has_many :pull_request_issues, -> {where(issue_classify: "pull_request")}, source: :issue, through: :issue_tags_relates
  belongs_to :project, optional: true, counter_cache: true
  belongs_to :user, optional: true
  belongs_to :organization, optional: true

  scope :pm_able, -> {where(project_id: 0)}

  validates :name, uniqueness: {scope: :project_id, message: "已存在" }, if: :pm_project?

  def pm_project?
    !project_id.zero?
  end

  def self.init_data(project_id)
    data = init_issue_tag_data
    data.each do |item| 
      next if IssueTag.exists?(project_id: project_id, name: item[0])
      IssueTag.create!(project_id: project_id, name: item[0], description: item[1], color: item[2])
    end
    $redis_cache.hset("project_init_issue_tags", project_id, 1)
  end

  def self.pm_init_data(pm_project_id)
    data = init_issue_tag_data
    data.each do |item|
      next if IssueTag.exists?(pm_project_id: pm_project_id, project_id: 0, name: item[0])
      IssueTag.create!(pm_project_id: pm_project_id, project_id: 0, name: item[0], description: item[1], color: item[2])
    end
    $redis_cache.hset("pm_project_init_issue_tags", pm_project_id, 1)
  end

  def self.pm_org_init_data(organization_id)
    data = init_issue_tag_data
    data.each do |item|
      next if IssueTag.exists?(organization_id: organization_id, project_id: 0, name: item[0])
      IssueTag.create!(organization_id: organization_id, project_id: 0, name: item[0], description: item[1], color: item[2])
    end
    $redis_cache.hset("pm_org_init_issue_tags", organization_id, 1)
  end

  def reset_counter_field 
    self.update_column(:issues_count, issue_issues.size)
    self.update_column(:pull_requests_count, pull_request_issues.size)
  end


  def  self.init_issue_tag_data
    [
      ["缺陷", "表示存在意外问题或错误", "#d92d4c"],
      ["功能", "表示新功能申请", "#ee955a"],
      ["疑问", "表示存在疑惑", "#2d6ddc"],
      ["支持", "表示特定功能或特定需求", "#019549"],
      ["任务", "表示需要分配的任务", "#c1a30d"],
      ["协助", "表示需要社区用户协助", "#2a0dc1"],
      ["搁置", "表示此问题暂时不会继续处理", "#892794"],
      ["文档", "表示文档材料补充", "#9ed600"],
      ["测试", "表示需要测试的需求", "#2897b9"],
      ["重复", "表示已存在类似的疑修", "#bb5332"]
    ]
  end

  def self.init_mp_issues_tags
    data = {"total_count": 10,}
    data["issue_tags"] = init_issue_tag_data.map{|item|
      {
        "name":  item[0],
        "description": item[1],
        "color": item[2],
      }
    }
    data
  end


  def to_builder 
    Jbuilder.new do |tag|
      tag.(self, :id, :name, :description)
    end
  end

end
