class IssueWebhookJob < ApplicationJob
  include ApplicationHelper
  queue_as :message

  def perform(issue_id)
    issue = Issue.find_by(id: issue_id)
    return if issue.blank?
    project = issue.project
    user = issue.user
    domain = "#{Rails.application.config_for(:configuration)['platform_url']}"
    gitea_domain = GiteaService.gitea_config[:domain]
    ssh_url = "git@#{gitea_domain.gsub("https://", "")}:#{project.owner.login}/#{project.identifier}.git"
    event_json = {
      "action": "opened",
      "number": 1,
      "issue": {
        "id": issue.id,
        "url": "#{domain}/api/v1/#{project.owner.login}/#{project.identifier}/issues/#{issue.id}",
        "html_url": "#{domain}/#{project.owner.login}/#{project.identifier}/issues/#{issue.id}",
        "number": 1,
        "user": {
          "id": user.id,
          "login": "#{user.login}",
          "full_name": "",
          "email": "#{user.mail}",
          "avatar_url": "#{domain}/#{url_to_avatar(user)}",
          "language": "",
          "is_admin": false,
          "last_login": "#{user.last_login_on}",
          "created": "#{user.created_on}",
          "restricted": false,
          "active": false,
          "prohibit_login": false,
          "location": "",
          "website": "",
          "description": "",
          "visibility": "public",
          "followers_count": 0,
          "following_count": 0,
          "starred_repos_count": 0,
          "username": "#{user.login}"
        },
        "original_author": "",
        "original_author_id": 0,
        "title": "#{issue.subject}",
        "body": "#{issue.description}",
        "ref": "",
        "labels": [],
        "milestone": nil,
        "assignee": nil,
        "assignees": nil,
        "state": "open",
        "is_locked": false,
        "comments": 0,
        "created_at": "#{issue.created_on}",
        "updated_at": "#{issue.updated_on}",
        "closed_at": nil,
        "due_date": nil,
        "pull_request": nil,
        "repository": {
          "id": 11307,
          "name": "#{project.identifier}",
          "owner": "#{project.owner.login}",
          "full_name": "#{project.owner.login}/#{project.identifier}"
        }
      },
      "repository": {
        "id": project.id,
        "owner": {
          "id": project.owner.id,
          "login": "#{project.owner.login}",
          "full_name": "",
          "email": "#{project.owner.mail}",
          "avatar_url": "#{domain}/#{url_to_avatar(project.owner)}",
          "language": "",
          "is_admin": false,
          "last_login": "#{project.owner.last_login_on}",
          "created": "#{project.owner.created_on}",
          "restricted": false,
          "active": false,
          "prohibit_login": false,
          "location": "",
          "website": "",
          "description": "",
          "visibility": "public",
          "followers_count": 0,
          "following_count": 0,
          "starred_repos_count": 0,
          "username": "#{project.owner.login}"
        },
        "name": "#{project.identifier}",
        "full_name": "#{project.owner.login}/#{project.identifier}",
        "description": "",
        "empty": false,
        "private": false,
        "fork": false,
        "template": false,
        "parent": nil,
        "mirror": false,
        "size": 843,
        "html_url": "#{domain}/#{project.owner.login}/#{project.identifier}",
        "ssh_url": "#{ssh_url}",
        "clone_url": "#{domain}/#{project.owner.login}/#{project.identifier}.git",
        "original_url": "#{project.repository.mirror_url}",
        "website": "",
        "stars_count": 0,
        "forks_count": 0,
        "watchers_count": 1,
        "open_issues_count": 1,
        "open_pr_counter": 0,
        "release_counter": 0,
        "default_branch": "master",
        "archived": false,
        "created_at": "#{project.created_on}",
        "updated_at": "#{project.updated_on}",
        "permissions": {
          "admin": true,
          "push": true,
          "pull": true
        },
        "has_issues": true,
        "internal_tracker": {
          "enable_time_tracker": true,
          "allow_only_contributors_to_track_time": true,
          "enable_issue_dependencies": true
        },
        "has_wiki": true,
        "has_pull_requests": true,
        "has_projects": false,
        "ignore_whitespace_conflicts": false,
        "allow_merge_commits": true,
        "allow_rebase": true,
        "allow_rebase_explicit": true,
        "allow_squash_merge": true,
        "default_merge_style": "merge",
        "avatar_url": "",
        "internal": false,
        "mirror_interval": ""
      },
      "sender": {
        "id": user.id,
        "login": "#{user.login}",
        "full_name": "",
        "email": "#{user.mail}",
        "avatar_url": "#{domain}/#{url_to_avatar(user)}",
        "language": "",
        "is_admin": false,
        "last_login": "#{user.last_login_on}",
        "created": "#{user.created_on}",
        "restricted": false,
        "active": false,
        "prohibit_login": false,
        "location": "",
        "website": "",
        "description": "",
        "visibility": "public",
        "followers_count": 0,
        "following_count": 0,
        "starred_repos_count": 0,
        "username": "#{user.login}"
      }
    }
    # puts "event_json:#{event_json.to_json}"
    begin
      url = URI("http://117.50.185.50:80")
      http = Net::HTTP.new(url.host, url.port)
      request = Net::HTTP::Post.new(url)
      request["Content-Type"] = "application/json"
      uuid = SecureRandom.uuid
      request['X-GitHub-Delivery'] = uuid
      request['X-Gitea-Delivery'] = uuid
      request['X-Gogs-Delivery'] = uuid
      request['X-GitHub-Event'] = 'issues'
      request['X-Gitea-Event'] = 'issues'
      request['X-Gogs-Event'] = 'issues'
      request.body = JSON.dump(event_json)

      response = http.request(request)
      Rails.logger.info "issue #{issue_id} webhook event======#{response.read_body}"
    rescue Exception => e
      Rails.logger.info "issue #{issue_id} webhook event error======#{e.message}"
      puts "issue: #{issue_id} webhook event error======#{e.message}"
    end
  end

end