# frozen_string_literal: true

module Mutations
  module Organizations
    class Create < Base
      graphql_name 'OrganizationCreate'

      authorize :create_organization
      authorize_granular_token permissions: :create_organization, boundary: :instance, boundary_type: :instance

      argument :name, GraphQL::Types::String,
        required: true,
        description: 'Name for the organization.'

      argument :path, GraphQL::Types::String,
        required: true,
        description: 'Path for the organization.'

      argument :visibility, Types::Organizations::VisibilityEnum,
        required: false,
        description: 'Visibility for the organization.'

      def resolve(args)
        authorize!(:global)

        result = ::Organizations::CreateService.new(
          current_user: current_user,
          params: args
        ).execute

        { organization: result.payload[:organization], errors: result.errors }
      end
    end
  end
end
