# frozen_string_literal: true

module Security
  module CiConfiguration
    class SastCreateService < ::BaseService
      def initialize(project, current_user, params)
        @project = project
        @current_user = current_user
        @params = params
        @branch_name = @project.repository.next_branch('add-sast-config')
      end

      def execute
        result = ::Files::MultiService.new(@project, @current_user, attributes).execute

        if result[:status] == :success
          result[:success_path] = successful_change_path
        end

        result
      end

      private

      def attributes
        actions = Security::CiConfiguration::SastBuildActions.new(@project.auto_devops_enabled?, @params).generate

        @project.repository.add_branch(@current_user, @branch_name, @project.default_branch)
        message = _('Add .gitlab-ci.yml to enable or configure SAST')

        {
          commit_message: message,
          branch_name: @branch_name,
          start_branch: @branch_name,
          actions: actions
        }
      end

      def successful_change_path
        description = _('Add .gitlab-ci.yml to enable or configure SAST security scanning using the GitLab managed template. You can [add variable overrides](https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings) to customize SAST settings.')
        merge_request_params = { source_branch: @branch_name, description: description }
        Gitlab::Routing.url_helpers.project_new_merge_request_url(@project, merge_request: merge_request_params)
      end
    end
  end
end
