module IssueLinks
  class DestroyService < BaseService
    def initialize(issue_link, user)
      @issue_link = issue_link
      @current_user = user
      @issue = issue_link.source
      @referenced_issue = issue_link.target
    end

    def execute
      return error('No Issue Link found', 404) unless permission_to_remove_relation?

      remove_relation
      create_notes

      success(message: 'Relation was removed')
    end

    private

    def remove_relation
      @issue_link.destroy!
    end

    def create_notes
      SystemNoteService.unrelate_issue(@issue, @referenced_issue, current_user)
      SystemNoteService.unrelate_issue(@referenced_issue, @issue, current_user)
    end

    def permission_to_remove_relation?
      can?(current_user, :admin_issue_link, @issue) &&
        can?(current_user, :admin_issue_link, @referenced_issue)
    end
  end
end
