class Webhook::PullCommentClient 
  include Webhook::Client 

  attr_accessor :webhook, :pull, :journal, :sender, :event, :action_type, :comment_json

  def initialize(webhook, pull, journal, sender, event, action_type='created', comment_json={})
    @webhook = webhook
    @pull = pull.reload
    @journal = journal.present? ? journal.reload : nil
    @sender = sender.reload
    @event = event 
    @action_type = action_type
    @comment_json = comment_json
    # 构建client参数
    super({
      uuid: SecureRandom.uuid, 
      event: @event, 
      webhook: @webhook, 
      payload_content: payload_content
    })
  end

  private 

  def payload_content
    payload_content = {
      "action": @action_type,
      "number": @pull.gitea_number,
      "pull_request": JSON.parse(@pull.to_builder.target!),
      "comment": @journal.present? ? JSON.parse(@journal.to_builder.target!) : @comment_json,
      "project": JSON.parse(@pull.project.to_builder.target!),
      "sender": JSON.parse(@sender.to_builder.target!),
      "is_pull": true
    }

    payload_content.merge!({
      "changes": @comment_json
    }) if @action_type == "edited"

    payload_content
  end 

end